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

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

admin
Site Admin
Posts: 10065
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 12663 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

admin
Site Admin
Posts: 10065
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:

admin
Site Admin
Posts: 10065
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

admin
Site Admin
Posts: 10065
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

admin
Site Admin
Posts: 10065
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.

admin
Site Admin
Posts: 10065
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.

admin
Site Admin
Posts: 10065
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: No registered users and 98 guests