• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Order of Initialization

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
This is a sample code, i am copying from Enthuware.

I can understand the answer is 0,4. But can someone explain in what order i is getting initialized.
Please help,
Arhaan
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess would have to be that i gets its default value before any of the constructors are called. So before any constructors are run the instance variables get their default values. That is why you see when new B() is called, A calls B's overriding print() method and value of 0 is printed.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,

Faraz is correct. What are you basically confused about? Which line?

BR,
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When this line of code will be executed?
Arhaan
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arhaan Singhania wrote:

When this line of code will be executed?
Arhaan



It is executed after call to B's super() call but before B's constructor is executed. Put the following code in class B:

 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faraz,

Thanks man, it really solved the confusion. Thankyou very much.

Arhaan
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic