Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Question on Constructor, instance var & static var

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a very basic (looking) question.
The output from the following code is :
ClassA constructor10
ClassB constructor50
ClassC constructor
// code begining

//
I am puzzled by the order.
I would appreciate if anyone could help me understand.
Thanks,
Swamy
(edited by Cindy to format code)

[This message has been edited by Cindy Glass (edited August 07, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When creating an instance of a class first the class is initialized, then the constructor is called.
In addition the constructions starts with the highest superclass and works its way down.
Since you start your app with the code in the main method the line
ClassC C = new ClassC();
Starts by loading ClassC and initializing its static variables (so now i=10).
Then it walks up the heiracrchy till it finds the top class and starts initializing the super class A (nothing is printed), and calling the constructor for A.
ClassA constructor10 is printed.
Then the initialization for ClassC is done.
j=50 and
ClassB constructor50 is printed.
Then the construcotr for ClassC is called.
ClassC constructor is printed.

[This message has been edited by Cindy Glass (edited August 07, 2001).]
 
Swamy Sivasubramaniyan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
That makes sense (now)!
Swamy
 
    Bookmark Topic Watch Topic
  • New Topic