• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of Variable declaration

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i have question about the following program.

Why the output is During Object followed by static instead of other way?

Since static variable is initialized first it should be displayed as static first followed by during Object right?



please explain
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the class is loaded, b is initialized. For that, it first creates an instance of the class, and that instance initializes its member "a". So the following is the exact order:
- creation of new Test instance
- initialization of its member "a" - first call to disp()
- that instance is used to call disp() again
- the result of that method is assigned to b
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic