About Question enthuware.ocajp.i.v7.2.1009 :
Moderator: admin
-
- Posts: 7
- Joined: Wed Mar 23, 2016 1:39 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
no the software mention QBANK as wrong answer
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Thu Jan 26, 2017 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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."
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."
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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 ?
I mean here, in this case, this initialization will not even be considered !
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 ?
I mean here, in this case, this initialization will not even be considered !
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
I mean, the initialization being made by the sub class is not considered right ? but it should be right !
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
ya. its not executed, when the purpose of of the initialization in sub class was to initialize it again
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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.
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.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
-
- Posts: 16
- Joined: Sun Jun 12, 2016 7:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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
I fail to understand, why "In Sub" is not printed.
Regards
Raj
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
Correct, the Sub class is not loaded. You missed this part of the explanation:
So, even if you use Sub dot syntax, Sub class is not loaded.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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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?
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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.
-
- Posts: 9
- Joined: Mon Feb 17, 2020 12:10 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1009 :
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.
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.
Who is online
Users browsing this forum: No registered users and 98 guests