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
About Question enthuware.ocajp.i.v7.2.1009 :
Moderator: admin
-
- Posts: 16
- Joined: Sun Jun 12, 2016 7:43 am
- Contact:
-
- Site Admin
- Posts: 10046
- 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: 10046
- 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: 10046
- 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 92 guests