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

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

Moderator: admin

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

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

Post by heleneshaikh »

Thank you very much Paul.

andrix
Posts: 2
Joined: Wed Oct 14, 2015 7:36 pm
Contact:

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

Post by andrix »

I know this was posted a long time ago.. but I just now stumbled upon this :)
So there was this question on 1st page of this thread:
Sweetpin2 wrote:But When we put below code in Sub class itself

public static void main(String[] args){
System.out.println(Sub.ID);
}

it prints In SubQBANK, why so?
And admin response was:
admin wrote:Because when you do Sub.ID, you are refering to the class Sub, and so Sub's static initialize is run, which prints "In Sub" and then QBANK is printed.
Well... I must say.. this answer may not be quite true.

First of all, as Sweetpin2 stated, he just copied code (main method) from class Test to class Sub so now we have 2 (equal) main methods in file (one in Sub and one in Test class). When we compile this file (e.g. run "javac Test.java" in cmd on Windows, in case file is saved as Test.java) we will get 3 .class files. Two of them (Sub.class and Test.class) have main methods.

Now, when we run main from Test (e.g. run "java test.Test" in cmd on Windows) we get "QBANK" as output. Just like answer of OCA question says.
But if we run main from Sub (e.g. run "java test.Sub" in cmd on Windows) we get "In SubQBANK" as output. And now.. this is what user Sweetpin2 noticed. But reason for this behavior is quite different, in my opinion.

The simplest explanation for this is that, when main method is executed in Sub class, that class gets loaded (instead of Test class, so "In Sub" gets printed) and only after that, Sub.ID is printed. In case method from Test class is run, class Sub is never loaded so "In Sub" is never printed. In both cases main method calls "System.out.println(Sub.ID);" so they are both refereing to class Sub but this has actually no effect on outcome in this case.

Note that this can easily can be tested in some IDE (like Eclipse) since IDE can automatically detect two main methods. When user asks IDE to run code as java Application it will ask user to specify main method to run (int this case: "Sub-test" and Test-test") and output will depend on user choice.

Sorry for long post.
And correct me if I am wrong :)

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

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

Post by admin »

You are right. I misunderstood what he was saying. Since the main method is in Sub, that class is loaded and because of that its static initializer is executed, which prints InSub.
Very sorry about the mistake and thanks for your feedback!
-Paul.
If you like our products and services, please help us by posting your review here.

Sergio
Posts: 7
Joined: Wed Mar 23, 2016 1:39 am
Contact:

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

Post by Sergio »

Please check this: https://ideone.com/xE636D
I see ONLY
QBANK

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

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

Post by admin »

That is indeed the correct option mentioned in the question as well.
Paul.
If you like our products and services, please help us by posting your review here.

Sergio
Posts: 7
Joined: Wed Mar 23, 2016 1:39 am
Contact:

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

Post by Sergio »

no the software mention QBANK as wrong answer

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

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

Post by admin »

Attached is what I see and this question hasn't been changed since 27 Nov 2013. Could you please check it again and confirm that you don't see option 2 as the correct answer?
Paul.
2.1009.png
2.1009.png (27.64 KiB) Viewed 12660 times
If you like our products and services, please help us by posting your review here.

PeterD
Posts: 4
Joined: Thu Jan 26, 2017 8:45 am
Contact:

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

Post by PeterD »

The java SE8 language specification is:

http://docs.oracle.com/javase/specs/jls ... l#jls-12.4

"A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface."

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

class Super { static String ID = "QBANK"; }

class Sub extends Super{

static {
ID="QB";
System.out.print("In Sub"); }
}

class test {

public static void main(String[] args) {

System.out.println(Sub.ID);


}

}

In the above code, ID is being initialized again by the subclass to another value. Isn't ignoring this initialization wrong ? :roll:

I mean here, in this case, this initialization will not even be considered ! :o

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

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

Post by admin »

Not sure what you mean by, "isn't ignoring this initialization wrong".
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

I mean, the initialization being made by the sub class is not considered right ? but it should be right ! :cry:

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

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

Post by admin »

Sorry, still no idea what you mean. What are you expecting and what is the difference between what you are expecting to happen and what is actually happening? What do you mean by "not considered". Not considered by whom? DO you mean it is not executed?
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

ya. its not executed, when the purpose of of the initialization in sub class was to initialize it again

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

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

Post by admin »

That is because the static initializer is executed only when a class is required to be initialized. Accessing a static field of a super class (even though using the name of a subclass) does not require the subclass to be loaded. In your code Sub.ID resolves at compile time itself to Super's ID. So there is no need to for the JVM to load and initialize Sub.

Section 12.4.1 of JLS clearly specifies the cases in which a class is initialized.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

ok tnx !

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

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

Post by raj_dp »

Understood that we can access a static member of the Super class by using Sub class name. But when we use the Sub class name, is the Sub class not loaded. And if the Sub class gets loaded its static block shall get executed and hence print "In Sub".
I fail to understand, why "In Sub" is not printed.
Regards
Raj

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

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

Post by admin »

Correct, the Sub class is not loaded. You missed this part of the explanation:
A reference to a static field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.
So, even if you use Sub dot syntax, Sub class is not loaded.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

Hi, I understand subclass is not loaded; therefore, no static initializer from sub class get executed.
But how could you call "Sub.ID" and get ID value from its parent class? Whats going on behind this?
Last edited by crazymind on Wed Jan 09, 2019 10:11 pm, edited 1 time in total.

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

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

Post by admin »

static calls to fields and methods are bound at compile time. The compiler knows that even though you are doing Sub.ID, ID is in super class and so, the compiler generate code for something like Super.ID. That is why the JVM does not load Sub.
If you like our products and services, please help us by posting your review here.

Dani1515
Posts: 9
Joined: Mon Feb 17, 2020 12:10 pm
Contact:

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

Post by Dani1515 »

ok, even when using only a final static variable from another class, the static initializer of this foreign class will not be invoked!!!

its a pitty that the book lacked that information in the chapter "Create and using methods, 10.5.6 Class loading and static initializers.
So there is no chance to answer this question correctly with the use of the book only. Its a serious omission, pls fix that.

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

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

Post by admin »

Hi Dani,
Thank you for your feedback. What topics need to be covered in the book is based on the judgement of the author. In general, I can tell you that Real Exam < Book < Mock exams. In other words, the book will cover more than what is required for the real exam and the mock exams will cover a little more than the book. This is because the mock exams are more dynamic than the book.

May be the author feels that this topic is not critical for the exam and so it was not included. Even so, the author has been notified about it.

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: admin, Google [Bot] and 94 guests