go to previous page   go to home page   go to next page



Work Behind the Scenes

The default constructor does a great deal of work "behind the scenes" when it is used. It works with the Java virtual machine to find main memory for the object, sets up that memory as an object, puts in it the variables and methods specified in the class definition, and returns an object reference to your program. All of this is quite complicated, and it is fortunate that you don't have to write code for it.

If you need to initialize some variables in a newly constructed object, then need to write a constructor as part of a class definition. In the constructor, all you need to do is write the statements that initialize the variables. The "behind the scenes" work still is included automatically.

Syntax Rule: If you define one or more constructors for a class, then those are the only constructors that the class has. The default constructor is supplied automatically only if you define no constructors.

This rule actually makes sense. If you are defining constructors for a class, then it is likely that you want to have control over the process and don't want an extra default constructor.


(Design Question: ) In our design so far, every HelloObject object prints the same string to the monitor. How could this class be improved?