• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of statics in base and subclasses

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry, my exam is tomorrow, so I won't be bombarding the forum with questions too much longer!! Sorry!

Anyway, quick question on the execution of static blocks if that's ok....

re-reading this thread, I was fairly sure I knew most of the concepts surrounding statics and instance initialisers.



so calling 'new B()' will call super(), to A, super() to Object, then come back down the hierarchy executing statics/instance blocks/the rest of the constructors. as above.

but this got me thinking - I thought statics *should* be run when the class is loaded. so why isn't B's static run prior to A's? if B's super() is called, the class must be loaded.

is it because two 'runs' up the hierarchy take place? so it goes up the hierarchy, executes the statics on the way back down, then calls super() all the way up, then executes the instance blocks, followed by the constructors (sorry, I can't think of a better way to describe this).

even so, I'm still not sure why A is loaded first - 'how' it is loaded first is probably the better question, in relation to the code.

sorry if the above isn't that clear, although I hope it is!

Nick
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that's what it means to load a class. When B is being loaded, it will first load super-types. The order is something like this:
 
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
nick woodward wrote:but this got me thinking - I thought statics *should* be run when the class is loaded. so why isn't B's static run prior to A's? if B's super() is called, the class must be loaded.

That's true! But when and how classes are loaded is determined by the JVM (and I'm pretty sure the JLS has a very nice section about it). Probably the JVM decides that in order to load a class, first all its subclasses have to be loaded. And that's why the static initializer of class A is executed before the one of class B. If you want to know which classes are loaded (and when) during the execution of an application, you can use the java command option -verbose:class. If we use this option when executing the Statics class, this is a part of the output
...
[Loaded Statics from file:/D:/home/java/projects/testing8/target/]
...
[Loaded A from file:/D:/home/java/projects/testing8/target/]
[Loaded B from file:/D:/home/java/projects/testing8/target/]
A's Static Init
B's Static Init
A's Init Block
A's Constructor
B's Init Block
B's Constructor
...

And as you can clearly see, the classes are loaded in this order: first Statics, followed by A and finally B. And that explains why the static initializer block of class A is executed first.

Pop quiz question time! What's the output of this code snippet (using the same class definitions of A and B, without using the -verbose command option)

Hope it helps!
Kind regards,
Roel
 
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
Roel De Nijs wrote:But when and how classes are loaded is determined by the JVM (and I'm pretty sure the JLS has a very nice section about it).

Loading the class file is indeed one of the things that happens first. And one of the next steps the class will be initialized (= executing its static initializers and the initializers for static fields (class variables) declared in the class). When these steps exactly happen are defined in different sections in the JLS. I'll provide a link just for completeness (this is definitely not on the OCA exams): 12.2. Loading of Classes and Interfaces and 12.4. Initialization of Classes and Interfaces
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Pop quiz question time!

A a = new A();
"A's Static Init"
"A's Init Block"
"A's Constructor"

B b = null;


(without the smiley face - i don't think it loads the class at all)


I hope..... I'm not sure I've got a good win rate with these pop-quizes!

Thanks to both of you though - I think I'd just got the 'statics, then blocks, then constructors' idea stuck in my head and forgotten about the situation with the parent/child objects and classes

 
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
nick woodward wrote:I hope..... I'm not sure I've got a good win rate with these pop-quizes!

No reason to doubt at all. Absolutely spot-on!

And what about this one? What's the output of this code snippet?
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's always reason to doubt! You're almost as sneaky with your questions as Paul is with enthuware - it's a good thing, keeps me on my toes!


"A's Static Init"
"Java rocks"


(i better be right if using this smiley!!)
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think ...it just prints Java Rocks!

because A.MSG is just accessing the compile time constant MSG.

Maybe ..If you use a new Operator and say new A(), then all of it will be printed.


A's static init
A's init block
A's constructor
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but to access A.MSG doesn't it need to load A? therefore the static block will run?

*edit.. yeah i suppose being a constant could mean that isn't necessary....

anyway, i agree that the init and constructor won't!
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static constants can be referenced without having to load the enclosing interface.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think you need to load the class to access the compile time constant.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see? sneaky!

let me quickly go and remove that smiley........

(Ramya - you're right, you don't, that's what stephan is confirming - and unfortunately what i just found out by running the code!)
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28). Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility §13.1, §13.4.9), and definite assignment (§16 (Definite Assignment)).


This is what JLS says...not sure if this would help.

let me quickly go and remove that smiley.......


dont remove it !!! it looks good
 
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
nick woodward wrote:"A's Static Init"
"Java rocks"


(i better be right if using this smiley!!)

Alas, it will only print "Java rocks!" Because it's a compile-time constant, the compiler will perform some optimization and replace all such constants with the actual value. So the compiled code will look something likeAnd therefore it's not needed to load the A class by the JVM.

nick woodward wrote:see? sneaky!

Yep! It's got my name written all over it!

Since we are all having so much fun, let's have another one

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:see? sneaky!

Yep! It's got my name written all over it!

don't make me have a few french beers when (if) i pass!

i'm assuming that A has to load this time because the compiler doesn't know the value of the StringBuilder at compile time (the reference, not the object, is final)

so same answer:
"A's Static Init"
"Java Rocks"



and if i get this one wrong, i'm going home!





 
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
nick woodward wrote:i'm assuming that A has to load this time because the compiler doesn't know the value of the StringBuilder at compile time (the reference, not the object, is final)

so same answer:
"A's Static Init"
"Java Rocks"



and if i get this one wrong, i'm going home!

No need to go home, you can stay in the saloon and have a few The answer and more importantly the explanation are both spot-on!

And we keep going. What about this one?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new StringBuilder("Java Rocks!") is called .. so only static block is loaded. because there is no new A() , the instance initializer and constructor will not be called. So the answer would be
A's Static Init
Java Rocks!

 
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
Ramya Subraamanian wrote:So the answer would be

Java Rocks!
A's Static Init

That's incorrect! It are indeed the only two lines which will appear in the output, but the order should be reversed.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same again for that one i guess!

A's Static Init
Java rocks!

because the class has to be loaded because MSG isn't final
 
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
nick woodward wrote:A's Static Init
Java rocks!

because the class has to be loaded because MSG isn't final

Excellent! Because it's not final, it's not a compile-time constant and therefore the class A will be loaded.

And finally this one
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler error

it's an instance variable
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are trying to access a non static variable from a static context, so it wouldnt compile.
 
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
nick woodward wrote:compiler error

it's an instance variable

That was another sneaky one, but you didn't get fooled!

I'm pretty sure to ace every question about this topic
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure to ace every question about this topic


this is either a typo or bragging Roel! (you've already passed )

yeah, thanks for that. really helpful!
now to take my last day enthuware

thanks again,

Nick
 
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
nick woodward wrote:
I'm pretty sure to ace every question about this topic


this is either a typo or bragging Roel! (you've already passed )

A typo of course! I never brag about myself (although I could as my score report reveals I answered all questions on this topic correctly )

Should have been: I'm pretty sure you'll ace every question about this topic

nick woodward wrote:now to take my last day enthuware

Enjoy! Best of luck!
 
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
Ramya Subraamanian wrote:you are trying to access a non static variable from a static context, so it wouldnt compile.

Indeed!

And here is an additional one for you
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(new A().MSG);

this statement will make the static block to be called.

so A's Static Init will be printed. then Instance initializer is called but now there's this sneaky new A().... its like new A()again being called when new A() is actually running .its something like



it should cause an error. So maybe the answer is

A's Static Init
then stack overflow error.
 
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
Ramya Subraamanian wrote:So maybe the answer is

A's Static Init
then stack overflow error.

Not maybe, but definitely. You are absolutely spot-on!
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:now to take my last day enthuware

Enjoy! Best of luck!

81%

But I honestly found that horrible. Was constantly behind on time, and rushed almost everything. And a couple of questions that i could never have done in the time limit/question. I REALLY hope the exam isn't like that! Although at least I now know I can do it under pressure!

Nick
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I REALLY hope the exam isn't like that! Although at least I now know I can do it under pressure!


All the Best Nick .I am sure you'll rock the exam

Not maybe, but definitely. You are absolutely spot-on!


My answers always, begin with "Maybe"...because I am not the compiler or the roelinator




 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers!


oh, and the comproeler doesn't know maybe
 
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
nick woodward wrote:81%

But I honestly found that horrible. Was constantly behind on time, and rushed almost everything. And a couple of questions that i could never have done in the time limit/question. I REALLY hope the exam isn't like that! Although at least I now know I can do it under pressure!Nick

The Enthuware mock exams are considered to be harder than the actual exam. So if you have 81% on the Last Day Test, you are very likely to pass the actual exam as well. This should be a real confidence booster! Now you know you are definitely ready for the exam and you are able to cope with the time pressure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic