This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructors" Watch "constructors" New topic
Author

constructors

Mike Doss
Greenhorn

Joined: Jan 24, 2003
Posts: 1
I am relatively new to OOP and have a question on constructors:
1) Do constructors actually "construct" the object in memory, or does it merely initialize the state of instance variables? I am under the impression that the "new" keyword actually "constructs" your object, and that the constructor is just a convenience that allows you to initialize the state of that object at the time of instantiation. However, a C++ book I skimmed mentioned in passing that a C++ constructor serves additional purpose other than mere intialization-- if this is also true for Java, I do not know. This leads me to my second question...
2) Why must a child call the parent's constructor-- super() -- at the FIRST LINE of its own constructor? I thought it might have to do with some kind of structural layering of objects (building from the top layer down), but if constructors in fact merely initialize the states of instance variables, it would seem unnecessary to require to have that first-line requrement.
Much thanks...
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Think of it this way: the 'new' operator allocates enough memory for the newborn object. It performs a bit of initialization, such as storing a pointer to a class representation structure, to which the new object belongs, in front of that allocated memory. This representation structure contains pointers to all non-static methods as well as a pointer to the patent class.
Before a constructor is activated, all initialization code is executed, first the initialization code of the parent class is activated (which in turn calls calls the init. code of its own parent etc.) If no initialization code for a member is present, the member variable is initialized to its default value.
Finally the body of the constructor is executed. There are three possibilities here, as far as the programmer is concerned:
1) the first line of the constructor calls one of its compadres: this( ... );
2) the first line of the constructor calls one of its superclass constructors: super( ... );
3) the first line of the constructor does something else.
In case 3) the compiler prepends another first line: 'super();'. So this boils down to only two possible situations:
1) the first line reads 'this( ... );'
2) the first line reads 'super( ... );'
If all constructors have a 'this( ... )' first line, the compiler will object, because construction of that object would result in infinite recursion, otherwise, no matter how many constructors you have defined, before one of your constructors starts, the constructor of the super class is activated. The result is, that the newborn object is initialized parent first, child last.
Just because a constructor can be seen as a function, a constructor can do much more than simple 'this.foo= foo'; initialization stuff.
Resource can be acquired, other objects can be instantiated etc. etc.
kind regards
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Mike Doss:
I am relatively new to OOP and have a question on constructors:
1) Do constructors actually "construct" the object in memory, or does it merely initialize the state of instance variables? I am under the impression that the "new" keyword actually "constructs" your object, and that the constructor is just a convenience that allows you to initialize the state of that object at the time of instantiation. However, a C++ book I skimmed mentioned in passing that a C++ constructor serves additional purpose other than mere intialization-- if this is also true for Java, I do not know. This leads me to my second question...
2) Why must a child call the parent's constructor-- super() -- at the FIRST LINE of its own constructor? I thought it might have to do with some kind of structural layering of objects (building from the top layer down), but if constructors in fact merely initialize the states of instance variables, it would seem unnecessary to require to have that first-line requrement.
Much thanks...

The new creates all the instance variables for each class up the chain starting at the top and working its way down. In the process, it runs the constructor code for each object starting at the top and working its way down. Let's take a simple example.

So even if all that is happening is instance variable initialization, order does matter.
So why must super be called first? Because ClassB is accessing a variable (and may run methods) that only exist in ClassA so the variables need to be allocated and the class loaded into memory before ClassB can do anything.
As to the first question, there is more to the "new" than just initializing variables. The class must be loaded by the ClassLoader in order to be interpreted into machine language instructions.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors