File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question on Constructor, instance var & static var Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on Constructor, instance var & static var " Watch "Question on Constructor, instance var & static var " New topic
Author

Question on Constructor, instance var & static var

Swamy Sivasubramaniyan
Greenhorn

Joined: Aug 07, 2001
Posts: 3
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).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Swamy Sivasubramaniyan
Greenhorn

Joined: Aug 07, 2001
Posts: 3
Thanks.
That makes sense (now)!
Swamy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Constructor, instance var & static var
 
Similar Threads
Question regarding encapsulation
Parsing a variable
Scope and classes/methods
Scope and array questions
Why runtime exception thrown is this code