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

static blocks

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little confused by static blocks. I've read various examples, but all seem to fall just short of what will get this to stick in my head.

Often the answer is that "static blocks run when the class is first loaded" - this is all well and good, but often there's no explanation of when classes are loaded, with many people simply directing the inquisitive to the rather cruel and unusual java specification :P

For example, this code:



I understand that the static inits are run prior to the constructors. But why is the Super Static run first? I would've thought the sub class is loaded prior to the superclass as it is referenced first. I'm pretty sure its not the physical order of the classes in the .java file, and it's not the class that is public. And I don't think it's because super(); is being called either - because then it would make more sense for the output to be Super Static1, Super Constructor, Sub Static1, Sub Constructor. So how can I logically describe the steps being performed above?

I read that the JVM collects all the static members for execution - but if someone could explain, outloud so to speak, the steps that are occurring that would be much appreciated. its really slowing my progress, and enjoyment :\

thanks!

nick
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,
It's complicated. And usually doesn't matter which is why a lot of people don't know it.

A good rule of thumb is that the class is loaded when it is first referenced/needed. In your example, the main method is in Super so it is needed immediately. (An instance isn't needed right away, but the class name itself is for main to exist)
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Nick,
It's complicated. And usually doesn't matter which is why a lot of people don't know it.

A good rule of thumb is that the class is loaded when it is first referenced/needed. In your example, the main method is in Super so it is needed immediately. (An instance isn't needed right away, but the class name itself is for main to exist)


That's what I thought too - but switching the main method to the sub class seems to make no difference either. Super Static is still called first.

I'm pretty worried about this topic. It will definitely come up, and it consistantly catches me out. In fact worse, its starting to muddle my knowledge on constructors and instance blocks as I desperately search for the logic behind what the JVM is doing.

And this is before I change the reference to a polymorphic one and confuse myself even further. I'm not wrong am I? These are all legitimate test questions, no?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you move the main method, it is a different reason. i think it is because the superclass is created before the subclass reference wise (not instance wise.)

The OCA does not ask about tricky edge cases like this. I took the beta and saw a ton of questions. You do need to know about the order of instance/constructor between super/sub classes. And you need to know that statics come first. You don't need to know the order of statics between super and subclasses. Please don't make yourself crazy with it.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:When you move the main method, it is a different reason. i think it is because the superclass is created before the subclass reference wise (not instance wise.)

The OCA does not ask about tricky edge cases like this. I took the beta and saw a ton of questions. You do need to know about the order of instance/constructor between super/sub classes. And you need to know that statics come first. You don't need to know the order of statics between super and subclasses. Please don't make yourself crazy with it.


okay, thanks a lot

so the order will always be: statics of all the classes in the hierarchy, then (multiple) super() calls will take you to the top of the inheritance tree, and then it will go 'instance block/constructor' respectively back down through the sub classes, correct?


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. And that's plenty to know on it's own!
 
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:so the order will always be: statics of all the classes in the hierarchy, then (multiple) super() calls will take you to the top of the inheritance tree, and then it will go 'instance block/constructor' respectively back down through the sub classes, correct?

If you want to test your knowledge about "execution order", have a look at the code snippet in [url=]this post[/url] and try to predict the output. If your prediction is spot-on, you'll know everything you need to know on this subject.

A little nice-to-know: if you want to know which classes are loaded and in which order, you could add -verbose:class as an option to the java command. You don't need to know this option (nor its output) for the exam. It's just for fun (and out of curiosity).
Assume this code snippetExecuting this command line statementresults in this output[Opened D:\home\apps\java\jdk1.7.0_75\jre\lib\rt.jar]
[Loaded java.lang.Object from path_to_jdk\jre\lib\rt.jar]
...
[Loaded java.lang.String from path_to_jdk\jre\lib\rt.jar]
...
[Loaded Super from file:/path_to_project/bin/]
[Loaded Sub from file:/path_to_project/bin/]
[Loaded java.lang.Void from path_to_jdk\jre\lib\rt.jar]
in main
[Loaded java.lang.Shutdown from path_to_jdk\jre\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from path_to_jdk\jre\lib\rt.jar]


So you'll see java.lang.Object is the 1st class being loaded (which makes sense, it's the mother class of all classes). And you'll see the String class being loaded as well, then plenty of classes are loaded and finally the Super and Sub classes are loaded.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic