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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
matheuscs
Posts: 5
Joined: Thu Nov 07, 2013 6:06 pm
Contact:

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

Post 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)?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

matheuscs
Posts: 5
Joined: Thu Nov 07, 2013 6:06 pm
Contact:

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

Post by matheuscs »

What about the main method?

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post by ElizabethCM »

Hi Paul,

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

nagarjuna19
Posts: 5
Joined: Sat Mar 17, 2018 6:21 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], marpiva and 73 guests