• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Variable initialization mystery...

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain me why the value of Derived.z is 3 even after
assigning z = 5 in init() Method of Derived Class (See Commented Line 34)
Whereas the Value of Derived.y is 4, which takes value from init().
Thanks
class Base22
{
int x = 1; // initializer
Base22()
{
System.out.println("Called Base Constructor ");
init(); // call init function
}
void init()
{
System.out.println("Called Base Init Method");
x = 2; // change value of x
}
}
class Derived22 extends Base22
{
int y; // no initializer
int z = 3; // initialized to 3

Derived22()
{
System.out.println("Called Derived Constructor ");
}

void init() // change values of y and z
{
System.out.println("Called Derived Init Method");
y = 4; z = 5; // Line 34
}
public static void main(String[] args)
{
// make a base and a derived and observe
Base22 b = new Base22();
System.out.println();
Derived22 d = new Derived22();
System.out.println("Base.x == " + b.x);
System.out.println("Derived.x == " + d.x);
System.out.println("Derived.y == " + d.y);
System.out.println("Derived.z == " + d.z);
}
}
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the Derived22 object, d, is created, the following sequence takes place:
1. Instance variables in the Base22 class and those in the Derived22 class are initialised to their default values(x,y, and z are initialised to 0).
2. The Base22 class constructor is called. This calls the init() function. Since the object 'this' is now of the Derived22 class, its init() method is called.
3. Inside the init() method, the values are assigned which are :- y = 4, z = 5.
4. Once the Base22 class initialisation has been done, the Derived22 class initialisation is started.
5. In this process, any explicit assignments done to the instance variables is carried out. As z has been assigned 3, this is carried out. But y has not been given any explicit value. It therefore keeps its earlier value of 4. That's how
you get the result.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chunky,

I have numbered the complete flow sequence of the program from #1 to #25 at the end of each line in the code.
I am sorry I could'nt get the line numbers in the code aligned up vertically for you, although i tried a few times.
The points to note are in line #17, you are assigning y to 4 and z to 5. You may wonder how, we can
assign y and z when they have not been yet declared. This is because, when a subclass has instance
fields, then before the control goes to the superclass, the fields in the subclass are all automatically
initialized to default values, that is 0 for integer values, 0.0 for float and double values and null for
object references.
Thereafter, in line #18, y is being declared. As y is already initialized to 4, no further assignment is
done. In line #19, z is assigned to 3, hence the value of z is changed from 5 to 3.
Hope that helps.
Niraj
[This message has been edited by niraj singh (edited March 10, 2001).]
[This message has been edited by niraj singh (edited March 10, 2001).]
[This message has been edited by niraj singh (edited March 10, 2001).]
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is interesting. So am I right if I draw the conclusion that constructors are always carried out BEFORE the initialization of instance variables?
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can not say it this way.
Initialization of instance variables are carried out BEFORE the Constructor of the class of the object to create.
BUT at the very first the Instance - initialization AND the Constructors of the BASECLASSES of the class of the object to create is carried out.
am i right??
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic