Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parent, Subclass, and static String

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line1,
when the static String 'ID' is associated with the subclass "Sub",
it will print the
a) static block "In Super" and
b) the static String "SuperStatic"

On line 2, when I invoke an instance of the subclass,
it will print both static blocks in parent and child and some object
com.java.inheritance1.Sub@1d256a73

Question: What is the origin of the object string com.java.inheritance1.Sub@1d256a73 ?




Question:
Is the reason that the object information is printed to the screen because 'ID' is declared as static but not being specifically referenced on line2?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:Is the reason that the object information is printed to the screen because 'ID' is declared as static but not being specifically referenced on line2?

No. It's simply because you ask yourself to print the Sub-instance to the standard output (on line 2).

Thomas Hauck wrote:What is the origin of the object string com.java.inheritance1.Sub@1d256a73 ?

The default implementation of the toString-method (in Object class). This implementation is used when you print an object without implementing its toString-method. It's the concatenation of the fully qualified name of the class, an @ and the hash code of the object.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I saw the code sample I expected a complete other question: why in the line 1 output the In Sub is not printed...
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a added a new line to class "Sub" line3.
When I execute this line of code in main
System.out.println(Sub.ID); // line 1

it will print the static block in "Super" and "Sub",
as well as the static String in Sub.




Question: Why doesn't the static block in the subclass

get executed (in the first scenario) when line 3 is not present
and
System.out.println(Sub.ID); // line 1
is executed?


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static initializer block is executed when the class is loaded for the 1st time. So all we need to do is verify which classes will be loaded to explain the output.

Let's start with the 2nd scenario, it's the easiest one The class variable ID in class Sub is hiding the class variable ID in class Super. We want to print the class variable ID from class Sub. So in order to load class Sub it's required to load its superclass Super first. That's why first the static initializer block from class Super is executed, followed by the static initializer block of class Sub and finally the value of class variable ID (of class Sub) is printed.

Now the 1st scenario. The code seems similar, only class Sub doesn't define a class variable ID. We want to print the class variable ID from class Super using class Sub. So you would expect the same will be happening as described in the 2nd scenario. But don't forget static variables (and methods) are tied to the class (and are not inherited). So you write Sub.ID, but the virtual machine is very smart and it knows that it's actually Super.ID, so there is no need to load class Sub. That's why the static initializer block of class Sub is not executed.

Let's introduce a 3rd and 4th scenario, hopefully as educational as the previous ones.
3/ What's the result (which output, which runtime exception or compilation fails) when you run this program?

4/ What's the result (which output, which runtime exception or compilation fails) when you run this program?


(ps. sorry for the late reply, I must have missed this one)
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the thorough explanation.

During my studies, this is what I have uncovered.

The order of execution is as follows:


1. Static blocks of the base class
(only once, in the order they appear in the class).

2. Static blocks of the class.

3. Non-static blocks of the base class.

4. Constructor of the base class.

5. Non-static blocks of the class.

6. Constructor of the class.

7. Derived class's static or non-static blocks are not executed if that class is not being used.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And 1 to 4 is repeated for every base class until the mother of all classes (Object) is reached.

7. Derived class's static or non-static blocks are not executed if that class is not being used.

Not 100% correct! To be more precise (and correct): the execution of the non-static blocks has nothing to do with a class being used or not, it's all about creating instances (or not). So in your 2nd scenario (with Sub.ID) the static initializer block of class Sub will be executed, but the non-static blocks won't.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic