• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block and empty block inside any class.

 
vikram Kesav
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

I am confused with the output of the programme.



Output:=

Foo static
Bar static
foo
Bar
foo
Bar

Thanks in advance.

 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some basic concepts for you to think:

Before java can execute any code those classes should be loaded in to JVM first. When a class is to be loaded if that class extends any other classe(s) those should be loaded prior to that (one at the top of the hierarchy will be loaded first and then down the hierarchy, in the order). When the class is loaded it's static initializers are run. And last when an instance of the class is created it's instance initializers are run in the order they appear in the class file. For a comprehensive explanation you may refer the JLS .

HTH.
 
Samrat Kakustam
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Check the code I made, comment the lines 1 - 5 in any order, execute and check the output each time. Hope that helps some way.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic