• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructors

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic