aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initialization sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initialization sequence" Watch "initialization sequence" New topic
Author

initialization sequence

Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
Hi All,

can someone please tell me the exact sequence of initialization in term of what gets initialized before what, if I have for example 2 classes say...

class Animal {}

class Dog extends Animal{}

Taking in consideration that BOTH classes contains:
- static blocks
- instant blocks
- static variables
- instant variables
- final variables
- no-arg constructors

and inside class Dog, there will be the method main which will initialize a new Dog object

thanks in advance for your help


Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
At the time of instance creation, if there is no constructor defined then a no arg constructor is created which calls to the no args constructor of the super class. here no arg constructor of class dog will call no arg constructor of class animal.

static init block executed at class loading time

instance init block runs after call to super constructor

when multiple init block of a single type occur in a class they run in order from top down

i hope this will help you
Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
Originally posted by sweety sinha:

static init block executed at class loading time

instance init block runs after call to super constructor

when multiple init block of a single type occur in a class they run in order from top down

i hope this will help you



Thanks Sweety for your help...so just to make sure I understood you correctly

1- Static initialization blocks of the Animal class will run first in the order from top down

THEN

2- Instance initialization blocks of the Animal class will run in the order from top down

THEN

3- Instance variables of the Animal class will run in the order from top down

THEN

4- Animal class constructor

THEN

5. The exact same sequence happens with the Dog class

Am I right here?

and if yes, what if Animal and/or Dog class contains Static or Final variables...when Static or Final variables will be initialized in the above sequence?

Thanks in advance for your help
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
I often wonder about this myself
Well, like you said, static initialization blocks
will run when the class is first loaded by the JVM.
Then what happens next Dog instance variables will be given their
default class values. Then the call to Animals constructor (super())
is invoked. Then when this call returns, your Dog instance variables will be given their initial values (say x = 9...) then I would imagine the
instance initialization block would run. I cant see it running before
your instance variables are initialized.
Let me look into it
best regards


SCJP 5.0
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Heres the example code:
<blockquote>code:
<pre name="code" class="core"> class Animal {
}
class Dog extends Animal {
int collarSize = 3;
{ collarSize = 10; }

public static void main(String[] args) {
new Dog();
}
}
</pre>
</blockquote>

And if you compile this and use the java bytecode disassembler javap like so (after you have compiled the Dog class using javac):
<blockquote>code:
<pre name="code" class="core"> javap -c Dog </pre>
</blockquote>
you get the following output:
<blockquote>code:
<pre name="code" class="core">
Compiled from "Dog.java"
class Dog extends Animal{
int collarSize;

Dog();
Code:
0: aload_0
1: invokespecial #1; //Method Animal."<init>" : ( )V
4: aload_0
5: iconst_3
6: putfield #2; //Field collarSize:I
9: aload_0
10: bipush 10
12: putfield #2; //Field collarSize:I
15: return

public static void main(java.lang.String[]);
Code:
0: new #3; //class Dog
3: dup
4: invokespecial #4; //Method "<init>" : ( )V
7: pop
8: return

}
</pre>
</blockquote>

You can see that in main, new is invoked which kicks off Dog's constructor.
Looking at the bytecode for the Dog constructor, Dog's "this" reference is pushed onto the operand stack and then Animal's constructor is invoked.
Next Dog's "this" reference is again pushed onto the stack. The int value 3 is also pushed onto the operand stack and then putfield assigns 3 to collarSize. The values are then popped from the stack. Finally 10 is assigned to collarSize and Dog's constructor returns.
So in a nutshell, it seems that instance variables are given their assigned values after the call to super() and before the instance init blocks run.
Correct me if I am wrong!
Javap is great to play around with if you have the time and interest.
Hhmm, maybe I have too much time on my hands
Best regards.
K

[ July 16, 2008: Message edited by: Keith Nagle ]
[ July 16, 2008: Message edited by: Keith Nagle ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi mahmoud,

the two steps 2, 3 belong together, that means the compiler doesn't differentiate the initialization order of variables and initialization blocks. See<blockquote>code:
<pre name="code" class="core">class Test{

int i = f(1);
{ System.out.print("2."); }
int j = f(3);
{ System.out.print("4."); }

int f(int k){
System.out.print(k + ".");
return k;
}

public static void main(String[] args){
new Test();
}
}
</pre>
</blockquote>will print: 1. 2. 3. 4.

The same pattern holds for static initialization: Just make in the example the variables, initialization blocks and f() static. The output will be the same.
it seems that instance variables are given their assigned values after the call to super() and before the instance init blocks run.
My example shows that this statement is wrong. If it were true, the output had to be 1. 3. 2. 4. !!
[ July 16, 2008: Message edited by: Ralph Jaus ]

SCJP 5 (98%) - SCBCD 5 (98%)
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
No. In the context of my code above, it is not wrong.
Look at the disassembler output from my last post.
Your instance init blocks don't give the instance
variables their values. They only print numbers.
if they did assign values, you would find that
they do so in the order of the statements.
best regards
[ July 16, 2008: Message edited by: Keith Nagle ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Keith,

an easy modification of my example<blockquote>code:
<pre name="code" class="core">class Test{
int i = f(1);
{ i = f(2); }
int j = f(3);
{ j = f(4); }

int f(int k){
System.out.print(k + ".");
return k;
}

public static void main(String[] args){
new Test();
}
}
</pre>
</blockquote>again yields the result 1. 2. 3. 4.

If the init blocks would be processed after the variable declarations/initializations, how would you explain this result ?

By the way: An initialization block is processed as a whole and sequantially. The compiler didn't pick out initializations of variables first, then println and so on !
[ July 16, 2008: Message edited by: Ralph Jaus ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Keith, the statement
it seems that instance variables are given their assigned values after the call to super() and before the instance init blocks run
is definitely wrong in general. It is correct in your example

int collarSize = 3;
{ collarSize = 10; }

because there is at first a variable declaration/initialization and afterwards the init block. And that's just the order the compiler takes.
If your int block would be followed by a declaration/initialization of a variable, say

int i = 7;

it would be processed next, that means after your init block !!
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Originally posted by Ralph Jaus:
Hi Keith, the statement is definitely wrong in general. It is correct in your example

int collarSize = 3;
{ collarSize = 10; }

because there is at first a variable declaration/initialization and afterwards the init block. And that's just the order the compiler takes.
If your int block would be followed by a declaration/initialization of a variable, say

int i = 7;

it would be processed next, that means after your init block !!

Yep, I should have stated that my statement pertains to my example, not in general.A simple typo - my bad!
Bear in mind that when you talk about the order and the compiler above, you really mean the JVM and not the compiler? Instance init blocks are executed at runtime, when new is invoked on an object
Kind regards,
K
Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
First, thanks all for your help and support..I highly appreciate it

I have modified the code posted in one of the posts above a little as shown below

<blockquote>code:
<pre name="code" class="core"> public class Dog extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

public static void main(String[] args){
new Dog();
}
}

class Animal {

int iA = fA(1);
{ System.out.println("2.SUPER"); }

int jA = fA(3);
{ System.out.println("4.SUPER"); }

static { System.out.println("instatic SUPER"); }
static int mA = fstaticA(5);

int fA(int k){
System.out.println(k + ".SUPER");
return k;
}

static int fstaticA(int k){
System.out.println(k + ".SUPER_Static_VAR");
return k;
}

}
</pre>
</blockquote>

and by executing

java Dog

I got

instatic SUPER
5.SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1.SUPER
2.SUPER
3.SUPER
4.SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass


which confirms that all static variables, blocks for all classes in the inheritance tree of the first initialized class (Dog) will run first, then instance variable and blocks will follow from the top down in the inheritance tree

So far so good, but when I tried changing the code again and making it as follows:

<blockquote>code:
<pre name="code" class="core">
public class Dog2 extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

public static void main(String[] args){
new Dog2();
}
}

class Animal {

int i = f(1);
{ System.out.println("2_SUPER"); }

int j = f(3);
{ System.out.println("4_SUPER"); }

static { System.out.println("instatic SUPER"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUPER");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUPER_Static_VAR");
return k;
}

}
</pre>
</blockquote>

and by executing

java Dog2

I got the following result:

instatic SUPER
5.SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1.SUPER
2.SUPER
3.SUPER
4.SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass

So I was wondering, shouldn't inheritance have played a role here especially that there are static and instance methods and vars with the SAME EXACT name?

Thanks in advance for your help
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi mahmoud,

good examples. This is a good way to prepare for SCJP.

When running Dog2 I get

instatic SUPER
5_SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1_SUBClass
2_SUPER
3_SUBClass
4_SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass

Please check your output.

The output shown here is due to overriding the method f. Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors the overriden method is used.

Static methods couldn't be overriden (just redefined). Therefore in the static part one time Animal.fstatic(5) is processed and later Dog2.fstatic(5).

You might also look what happens at the following example<blockquote>code:
<pre name="code" class="core">class A{
int i = 1;
int j = f();
{ System.out.println(j); }

int f(){
System.out.println("A i = " + i);
return i;
}
}

class B extends A{

int i = 2;
int j = f();

int f(){
System.out.println("B i = " + i);
return i;
}

public static void main(String[] args){
new B();
}
}
</pre>
</blockquote>
[ July 17, 2008: Message edited by: Ralph Jaus ]
Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
Originally posted by Ralph Jaus:

The output shown here is due to overriding the method f. Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors the overriden method is used.
[ July 17, 2008: Message edited by: Ralph Jaus ]


Thanks Ralph for your help and kind words...

Kindly can you elaborate the above paragraph as I am still confused here about how overriding did effect the initialization sequence in the code?

Thanks in advance for your time and efforts
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors of the super class the overriden method is called.

But what exactly are you confused about ?
[ July 17, 2008: Message edited by: Ralph Jaus ]
Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
Originally posted by Ralph Jaus:
Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors of the super class the overriden method is called.

But what exactly are you confused about ?

[ July 17, 2008: Message edited by: Ralph Jaus ]


Let me see if I understand you correctly, if I have class Animal and its sub-class Dog, where Dog 'override' all instant variables, methods and blocks of class Animal...

So when I initialize Dog, will this mean that the instant variables, methods and blocks of class Dog (which are already an overridden instant variables, methods and blocks of class Animal) will run first?

This is what I am exactly confused in, I am not sure what happens when there is an overridden (Instant variable OR block OR method)...what will its sequence in initializing in the sub-class AND the super class

That is exactly what I am confused about here
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Only methods can be overriden, not variables, initialization blocks or constructors. I just tried to explain which methods were called during the instantiation of Dog2.

We should separate two questions:

1. In which order are static variable initializations, static initialization blocks, non-static variable initializations, non-static initialization blocks and constructors processed by the JVM ?

-> I think the order is cleared in the posts above.

2. If these artefacts in the super class use a method of the super class that is overriden in a sub class, which method is called during runtime when an instance of the sub class is created ?

-> the overriden method.
[ July 17, 2008: Message edited by: Ralph Jaus ]
Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
Ralph, thanks again everything is clear for me now Just one last question please

When I try to execute the following code

<blockquote>code:
<pre name="code" class="core"> public class DogVer3 extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

int z = g(99);
{ System.out.println("6_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

int g(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

DogVer3(){
super();
System.out.println("SUB Const.");
dogConst();
}
static void dogConst(){
System.out.println("SUB Const. II");
}

public static void main(String[] args){
new DogVer3();
}
}

class Animal {

int iA = fA(1);
{ System.out.println("2_SUPER"); }

int jA = fA(3);
{ System.out.println("4_SUPER"); }

int zA = g(99);
{ System.out.println("6_SUPER"); }

static { System.out.println("instatic SUPER"); }
static int m = fstatic(5);

int fA(int k){
System.out.println(k + "_SUPER");
return k;
}

int g(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUPER_Static_VAR");
return k;
}

Animal(){
System.out.println("Super Const.");
animalConst();
}
static void animalConst(){
System.out.println("Super Const. II");
}

}
</pre>
</blockquote>

I get

instatic SUPER
5_SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1_SUPER
2_SUPER
3_SUPER
4_SUPER
99_SUBClass
6_SUPER
Super Const.
Super Const. II
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass
99_SUBClass
6_SUBClass
SUB Const.
SUB Const. II

Why 99_SUBClass did appear (Twice)? I mean why it was called twice here?

Thanks in advance for your time and efforts
Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
I had a typo mistake in the previous code so here is it again, and the results are still the same

<blockquote>code:
<pre name="code" class="core"> public class DogVer3 extends Animal{

int i = f(1);
{ System.out.println("2_SUBClass"); }

int j = f(3);
{ System.out.println("4_SUBClass"); }

int z = g(99);
{ System.out.println("6_SUBClass"); }

static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);

int f(int k){
System.out.println(k + "_SUBClass");
return k;
}

int g(int k){
System.out.println(k + "_SUBClass");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}

DogVer3(){
super();
System.out.println("SUB Const.");
dogConst();
}
static void dogConst(){
System.out.println("SUB Const. II");
}

public static void main(String[] args){
new DogVer3();
}
}

class Animal {

int iA = fA(1);
{ System.out.println("2_SUPER"); }

int jA = fA(3);
{ System.out.println("4_SUPER"); }

int zA = g(99);
{ System.out.println("6_SUPER"); }

static { System.out.println("instatic SUPER"); }
static int m = fstatic(5);

int fA(int k){
System.out.println(k + "_SUPER");
return k;
}

int g(int k){
System.out.println(k + "_SUPER");
return k;
}

static int fstatic(int k){
System.out.println(k + "_SUPER_Static_VAR");
return k;
}

Animal(){
System.out.println("Super Const.");
animalConst();
}
static void animalConst(){
System.out.println("Super Const. II");
}

}
</pre>
</blockquote>
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Ralph Jaus:
Only methods can be overriden, not variables, initialization blocks or constructors.


Ralph,
What's the point in having protected variables , when the whole concept is about overriding them in subclass(in a diff. package) - when they can't be overridden.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Mahmoud,
Why 99_SUBClass did appear (Twice)?
The first time it appears because of the statement "int zA = g(99);" in class Animal.
But I see, I've to correct my quote above. Sorry, if it led to confusion. It has to be
2. If these artefacts in the super class (Animal) use a method of the super class (g) that is overriden in a sub class (DogVer3), which method is called during runtime when an instance of the sub class is created ? -> the overriding method (that means g in DogVer3).
The second time it appears because of the statement "int z = g(99);" in class DogVer3.
[ July 17, 2008: Message edited by: Ralph Jaus ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Nabila,

private, protected, public and so on have to do with visibility in other classes / packages, but not with overriding (the only connection is that an overriding method is not allowed to have a more restrictive access modifier than the overriden method).

If a variable of a class A is protected, than you can access it in a class B that resides in another package only if B extends A.
Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
Thanks Ralph so much for your help
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Nice topic.
Everything of initialization sequence needed for SCJP is here.


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am still kind of confused about the subclass initialisation taking place before the super class init block

When running Dog2 I get

instatic SUPER
5_SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1_SUBClass
2_SUPER
3_SUBClass
4_SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hi Nabila, let me try to explain you..

The subclass is not taking place, its the superclass, like it should, and like you are thinking :-) But the thing here is, the method of superclass was overrided by the subclass, so, when running the superclass initialization block, its using the subclass method instead of the superclass one, and making you think that its the subclass initialization.

Understand now? If not, I'll try to make it more clear later..
[ July 17, 2008: Message edited by: Raphael Rabadan ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Raphael Rabadan:
Hi Nabila, let me try to explain you..

The subclass is not taking place, its the superclass, like it should, and like you are thinking :-) But the thing here is, the method of superclass was overrided by the subclass, so, when running the superclass initialization block, its using the subclass method instead of the superclass one, and making you think that its the subclass initialization.

Understand now? If not, I'll try to make it more clear later..

[ July 17, 2008: Message edited by: Raphael Rabadan ]


Hi Raphael,
Firstly thanks for your time and patience, I honestly do appreciate it.

I guess I figured out what you are trying to tell me...Somewhat.

int i = f(1); //Super class

This statement is being executed in the parent class, but the f() method it's using is that of the subclass because it's overridden by the subclass.
I suppose that's what you meant.

What I cannot get is why is it happening this way.
I do know it's overridden , but i still would expect the parent class methd to get executed.


The only conclusion I came up with is..

Dog1 d=new Dog1(); OR Animal d=new Dog1();
d.f(1);

In both the cases, Base method is called and not the parent class.
So if you have a base class object and an overriden method then the base class will be executed.

Am I correct or Am i mixing something entirely differnt?

[ July 17, 2008: Message edited by: Nabila Mohammad ]
[ July 17, 2008: Message edited by: Nabila Mohammad ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Nabila,
I suppose that's what you meant.
Yes, your understanding of Raphaels explanation is correct.
Dog1 d=new Dog1(); OR Animal d=new Dog1();d.f(1);
In both the cases, Base method is called and not the parent class
Can you please show the whole code.
[ July 18, 2008: Message edited by: Ralph Jaus ]
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hello Nabila,

as Ralph said, you did understand what I said.

Originally posted by Nabila Mohammad:


...


The only conclusion I came up with is..

Dog1 d=new Dog1(); OR Animal d=new Dog1();
d.f(1);

In both the cases, Base method is called and not the parent class.
So if you have a base class object and an overriden method then the base class will be executed.

Am I correct or Am i mixing something entirely differnt?

...


I would ask you an example of what you are trying to say, because, for me, Base is the type you declared, and not the type of the instance . If it is what you meant, you're missing some OO points.

Because if you call a overridden method, it will call the instance method instead of the base. What you said apply for overload!

But, maybe i understand what you've said wrong. If not, take a look on this other javaranch's topic


By the way, i have another situation to you try to figure out. What would happen if the instance method/static method you are using to intialize an instance variable/static variable uses other instance variable/static variable that wasn't intialized yet.

After you end thinking... try it out:



after you've tried the last one.. try this new one:




That's amazing how of any topic in java we can keep getting more and more deep.
[ July 18, 2008: Message edited by: Raphael Rabadan ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hi,

Oh Boy - the deeper you get into it, more complicated it becomes and more questions you have.. Phewww!!!


For the first part of your code -

It was almost similiar to the previous code Almost..not quite..
overridden methof of the sub clas was executed for the method g()from SuperClass

What i did'nt understand was that:

1) How was the instance varibles of the Super class getting initilaised before running the super class constructor and same goes for the Base constructor.

2)While priniting out the value of "m" - it gave a compiler error
However after commenting it - Animal.m and DogVer.m both were executed and were given default values even though m wasnt declared or initialised at that time.

3) And why was "m" given a compile time errr when DogVer3.m was running.


And as for the second Code..
It's completely out of my mind or perhaps the compiler has gone Nuts!


-For some weird reason Instatic SUPERwas printed twice

-While callng method fstatic() of SUper class - it gives the result 5_SUPER_STATIC_VAR

and not 55_SUPER_STATIC_VAR -OR- 5_SUBCLASS_STATIc_VAR

- after fstatic(), it skipped the static init block of superclass and moved on tothe static init block of Sub Class..

THere are way too many questions...

Either I am mactching the result with wrong code or god knows what..

It lookes like it was juggling from the Super class to the sub class or somethin gof tht sort..


Phewwww ! Gets more complicated!


Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Ok, let's see what's happening


1) How was the instance varibles of the Super class getting initilaised before running the super class constructor and same goes for the Base constructor.


The instance variables were getting the default value before itget initialized.


2)While priniting out the value of "m" - it gave a compiler error
However after commenting it - Animal.m and DogVer.m both were executed and were given default values even though m wasnt declared or initialised at that time.


It really was meant to give a compile error, just to we play a bit ;-) It was given a default value because it wasnt initialized, anyway, we were using a direct acess, Class.staticField and it has it, if ran our static block that changes the value of the field or not, doesn't matter, will just get default values java put for variables. 0, 0.0d, null...


3) And why was "m" given a compile time errr when DogVer3.m was running.


"m" didn't exists yet for that context, but DogVer3.m and Animal3.m did.


-For some weird reason Instatic SUPERwas printed twice


It's because the static field "m" you are accessing might be from other class, Like here, I had a DogVer4 using DogVer3.m and got the same error ;-P.


-While callng method fstatic() of SUper class - it gives the result 5_SUPER_STATIC_VAR and not 55_SUPER_STATIC_VAR -OR- 5_SUBCLASS_STATIc_VAR


Sorry for it. I made a typo on the 1st version, i used 5 on both, Parent and Child.
The 2nd version give's 55_SUPER_STATIC_VAR, like it should.



- after fstatic(), it skipped the static init block of superclass and moved on tothe static init block of Sub Class..


It moved for the SubClass because we wanted to access a Static Field there, by calling his "m" and needed to load it, so thats why it did it.


I think that's all.. hehehe
And relax, you know too much of initialization for the exam. Even more that needed ;-P
[ July 18, 2008: Message edited by: Raphael Rabadan ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Wow...
That was a pretty long discussion that got my mind sizzling..But had a great time all the same!

Well, if I know enough then I guess I ll move on and spend time on more important stuff which I don't have much clue about like Genrics and Collections..


Wish me Luck.



And thanks for your time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initialization sequence