Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Sun Nov 24, 2013 8:03 pm
by matheuscs
Where does the static methods go in the list below?
The order of initialization of a class is:
1. All static constants, variables and blocks.(Among themselves the order is the order in which they appear in the code.)
Does the static methods come after all this list (not considering the order it appear in the code)?

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Sun Nov 24, 2013 8:19 pm
by admin
A method is invoked when there is an explicit call to that method in the code. If there is no explicit call the method will not be invoked at all. So there is no fixed "order" of when any method may be called.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Sun Nov 24, 2013 8:24 pm
by matheuscs
What about the main method?

When the class is called the main comes last, right?

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Sun Nov 24, 2013 9:05 pm
by admin
No, main method is called only if that specific class is executed from the command line. In this case, you are explicitly telling the JVM to call the main method by specifying the class name on the command line.

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Wed Jun 03, 2015 3:25 pm
by ElizabethCM
Hi Admin,

When you say:
"Which is the correct sequence of the digits that will be printed when B is run?"
it means B is run from the command line?

If so, when does the main method interfere?

I am not sure I think like the compiler :ugeek: , please correct me if I am wrong:
static A s1 = new A(1); // is called 1st as it is static and the 1st in the list
static A s2 = new A(4); // is called as the 2nd static intialization
Here is where I am confused:
Am I jumping to main because it's static? Or do I continue with the instance variable initialization A a = new A(2);?

Looking at the answer (which I guessed because 1 and 4 get printed first), why don't they follow by 3 and then 2?

Can you please explain how (and why) are they run?

Thanks a lot!

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Wed Jun 03, 2015 7:55 pm
by admin
Yes, it means run from the command line.

Before the JVM can invoke the run method of any class, it has to load that class. As a part of loading the class, it has to initialize the static variables and execute static initializer blocks.This happens only once. Once this is done, main method is invoked.
In this case, by the time main is invoked, 1 and 4 are already printed (because of initialization of static variables of B).

Instance variables and instance blocks are executed whenever an instance of a class is created. In this case, in the main method, the first statement is B b = new B(); i.e. you are creating an instance of B. So all the instance variables of B and its super classes will be initialized (first A's fields, and then B's fields). A doesn't have any such field, but B has : A a = new A(2). Therefore, 2 is printed.

The last line in main is A a = new A(3);, so 3 is printed.
HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Thu Jun 04, 2015 3:24 pm
by ElizabethCM
Hi Paul,

Thanks for the reply, it is clear now why 2 is printed before 3.

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Mon Mar 19, 2018 9:30 am
by nagarjuna19
Hi Admin,

Here Class A is not super class of B, so how we are calling class A as super class.
Explanation please.

"So all the instance variables of B and its super classes will be initialized (first A's fields, and then B's fields)."

Thanks,
Nagarjuna

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Mon Mar 19, 2018 10:40 am
by admin
Where did you read this statement that you've quoted, "So all the instance variables of B and its super classes will be initialized (first A's fields, and then B's fields)."?

Please post screen shot.

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Sat Sep 22, 2018 2:51 am
by flex567
In the explanation the order of initlialization for a class is this:

Code: Select all

The order of initialization of a class is:
1. All static constants, variables and blocks.(Among themselves the order is the order in which they appear in the code.)
2. All non static constants, variables and blocks.(Among themselves the order is the order in which they appear in the code.)
3. Constructor.
But why is there no mention of initialization of a superclass?

Re: About Question enthuware.ocajp.i.v7.2.1170 :

Posted: Sat Sep 22, 2018 7:13 am
by admin
Since this question does not use inheritance, the author has probably not mentioned it in this question to keep the explanation simple and to the point. The whole process is as follows -

The complete order of initialization while instantiating an object of a class is as follows -

1. All static constants, variables, and blocks. Among themselves the order is the order in which they appear in the code. This step is actually a part of "class initialization" rather than "instance initialization". Class initialization happens only if the class is being used for the first time while being instantiated. For example, if you have invoked a static method of a class or accessed a static field of that class earlier in the code, you have already used the class and the JVM would have performed initialization of this class at that time. Thus, there wouldn't be any need to initialize the class if you instantiate an object of this class now.

Further, if the class has a superclass, then the JVM performs this step for the superclass first (if the superclass hasn't been initialized already) and only after the superclass is initialized and static blocks are executed, does the JVM proceed with this class. This process is followed recursively up to the java.lang.Object class.

2. All non static constants, variables, and blocks. Among themselves the order is the order in which they appear in the code.

3. Constructor.

Just like the class initialization, instance initialization also happens for the superclass first. That is, if the class has a superclass, then the JVM takes steps 2 and 3 given above for the superclass first and only after the superclass's instance members are initialized, does the JVM proceed with this class. This process is also followed recursively up to the java.lang.Object class.

HTH,
Paul.