• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialization order  RSS feed

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new in this forum and I have my first question

Right now I'm studying the initialization order in Java. The question is fairly simple: Why the first line in the output of this code is 0 (zero):




I know the rules (1 - Superclass (If there is one), 2 - Static initializers, 3 - Instance initializers, 4 - Constructor). But the thing I don't understand is why the 0 is printed before "read to construct".

Thanks in advance

 
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jonathan,
Welcome to CodeRanch!
That's because static blocks are always executed first when a class is loaded in JVM then after it the main method is called and i think you know the rest things.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recommendation-http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4

Read this specification.it would clear your several doubts.

Kind regards,
Praveen.
 
Jonathan Pérez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer Praveen, already got my head around the concept
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!