• Post Reply Bookmark Topic Watch Topic
  • New Topic

why does the instance variable take value from constructor rather than instance block  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Instance variable can be initialized from constructor and same is the case with instance block but what if both are given at the same time? I tried to understand this with a basic example which is given below and the instance value is being taken from constructor.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you change your code to add a bit of output in the initialiser/constructor blocks then you'll see the order of execution.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Dave thanks for the reply, the order is Instance block->constructor block which is known(correct me if am wrong),what i was trying to understand is if the instance variable can be initialized from both instance block and constructor, what is the reason that the instance variable is getting initialized by constructor if  both constructor and instance blocks are used.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the constructor runs last, so what you'll see in the member variable at the end is the value that was set last - which is the value set by the constructor.

So, first the instance initializer runs, which sets a to the value 10, but then the constructor runs which sets a to the value 5 (overwriting what the instance initializer did) => so at the end a has the value 5.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Jesper

That means initially the value of the instance variable is the value initialized by instance block..if so is there a possibility to view the first set value?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. But not after the object is constructed of course, because then the value set by the initialiser has been overwritten by the constructor.

You can even see that a = 0 before the initialiser has done its work:


Output:

Initialiser; current value a = 0
After initialiser a = 10
Constructor; current value a = 10
After constructor a = 5
the value is:5
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a great example Jesper, i now clearly understand the concept...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!