• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

init blocks problem

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I have missed something learning about init blocks or constructors. Can anyone explain me the order in which executes XClass?
Assume we have a code



I know that the result will be DemoC++. But what if we change the location of init block in XClass.



Why the result is DemoJava?
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance blocks run in the order that they appear in the source file. Apparently the initialization of instance variables fall into the same category as instance blocks. So when you move the instance block to before the declaration and initialization of the instance variable, both of them happen, but in a different order, so the init of the instance variable occurs last and that is the result of the variable s.
 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are saying that instance blocks run in the order that they appear. But what if we add one more init block {s="C#";} in YClass.


According to the previous rule the result should be DemoC++ , cause int block runs after the main method in YClass .
But JVM says that the result is DemoC#. Why?

 
Ranch Hand
Posts: 71
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because C# value overwrites previous value.

This instance block will at after all instance blocks but before constructor.
 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got confused. I know that init block executes before constructor, but I don't understand in what order they run separately in Superclass and subclass when code has methods or variables?
You' ve said that in XClass instance blocks run in the order that they appear. It means that {s="C++";} run first and if it was printed after String s="Java"; it would have executed after String statement.
Doesn't it contradict the rule which tells that init block executes before constructor and the rest of the code?

Again I'm trying to understand the sequence how does the code executes. Correct me if I'm wrong.
For example we have a class with one init block, and one method. As I know all classes has implicit constructor. It means that when you instantiate an object the code in an init block runs first.
Then JVM executes constructor if it is explicit and has some code inside and only then JMV runs code inside method.
Everything is ok when I look at a YClass. But when I look at the XClass I see that init block runs in the order that they appear, but not before constructor and the rest of the code.

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you have the super-sub class relationship and both class have their respective static and instance initializer blocks .
Then they run in following order :

static blocks of super class
static blocks of sub class
instance blocks of super class
instance blocks of sub class
CONSTRUCTOR of super class
CONSTRUCTOR of sub class.

note at in each stage if there are more than one block they run in specified order.
 
Jagdev Singh
Ranch Hand
Posts: 71
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naveen I think the order should be following.

Static block of Super class.
Static block of Sub class.
Instance block of Super class.
Constructor of Super class.
Instance block of Sub class.
Constructor of Sub class.
 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote:when you have the super-sub class relationship and both class have their respective static and instance initializer blocks .
Then they run in following order :

static blocks of super class
static blocks of sub class
instance blocks of super class
instance blocks of sub class
CONSTRUCTOR of super class
CONSTRUCTOR of sub class.

note at in each stage if there are more than one block they run in specified order.




The order specified above is not correct. Sorry for mistake. Correct is poasted by Jagdev.

Regards
naveen
 
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic