Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTips MockExam No#39

 
Sivalingam Sivasuthan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I Got this Question From JTips Mock Exam No#39

The Answer is ofcourse 1. Can any body Plesae explain this.
Thanks in Advance.
Siva
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boy this was a good one.
This is what I understand:
1. New Processor calls the the constructor Processor in
line: Processor p = new Processor()
2. Processor() {
// System.out.println("Processor this.b " + this.b);
System.out.println("Processor constructor Value of b = " + b); }
The Processor() constructor is called, and then the super
class is called automatically, Process().
3. In Process() the method methodA is called. MethodA in
in Processor is called. I am sure this is done, but I am
not sure why. I believe it is because, MethodA in
Processor is the active definition of MethodA since it
extends Process.
Also, factually this.b has a value of 0. I am guessing, but
the object has not been fully instantiated at this point so
this.b has a value of 0. (Note after the constructor
Processor() is executed this.b will have 126.)

4. When methodA completes, Processor() is completed.
I hope this helps out. If you find out anything else please
let me know.
 
Chaitali Deshpande
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Process1 {
byte x=-1;

Process1() {
this.methodA();
}

void methodA() {
System.out.println("Value of b is = " + x );
}

public static void main(String [] args) {
Process1 p1 = new Process1();//additional line
Processor p = new Processor();
}
}

class Processor extends Process1 {
byte b=126;

Processor() {
System.out.println("Value of b = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
o/p is
value of b=-1
value of b=0// why this
value of b=126

now the object Process is instansiated still why do we get
b=0
in o/p.
 
Sivalingam Sivasuthan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Charlie Swanson,Chaitali Deshpande For your replys
I agree with Charlie Swanson Explanations.
To Chaitali Deshpande.
I think, Your are getting that -1 is form a separate Instance of Process1.
Siva.
[This message has been edited by Sivalingam Sivasuthan (edited January 30, 2001).]
 
Paul Wilson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charlie Swanson:
Boy this was a good one.
This is what I understand:
1. New Processor calls the the constructor Processor in
line: Processor p = new Processor()
...
I hope this helps out. If you find out anything else please
let me know.

____________
I posted a similar problem in the Java in General(Advanced) forum. The JLS explains this problem. As I remember it the title of the post is "order of initialization with static, instance initializers" or some such. Look under my name.
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chaitali Deshpande:
class Process1 {
byte x=-1;

Process1() {
this.methodA();
}

void methodA() {
System.out.println("Value of b is = " + x );
}

public static void main(String [] args) {
Process1 p1 = new Process1();//additional line
Processor p = new Processor();
}
}

class Processor extends Process1 {
byte b=126;

Processor() {
System.out.println("Value of b = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
o/p is
value of b=-1
value of b=0// why this
value of b=126

now the object Process is instansiated still why do we get
b=0
in o/p.


Chaitali th eanswer now is for the first object created
values is -1
value is 0
value is 126
is correct although i agree is little confusing
now read the expalantion carefully
An object is created and initialized only after all the constructors of the class hierachy is called
in the first case new Process1() s.o.p is called in the subclass itself so it completes execution of super class constructor (in this case Object) and then initialises b to -1
In the second case method call comes from the super class constructor itself and the overridden method of subclass is called.but at this time superclass constructor hasnt finish execution and the variable of subclass is not initialised and has a default value of 0.
now u r again using s.o.p in the subclass constructor and now the supercalss constructor is over and variables are initialized
Cherry

 
Chaitali Deshpande
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks cherry for ur detailed explanation.
I understood.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic