Next: Design methodology for a
Up: Unit 03
Previous: Overloading of constructors
When we create an object of a class that does not contain any constructor
definition (such as the first version of the Person class), the
so-called standard constructor is called.
- The standard constructor is a constructor without arguments
generated automatically by the compiler for all those classes that do not
contain any constructor definition.
- It leaves the instance variables initialized to their default
value, which is the value assigned automatically by the run-time support
when the memory location associated to the variable is being reserved.
- The standard constructor is automatically inhibited by the compiler when
the definition of any constructor (with or without arguments) is explicitly
present in the class. In particular, the programmer can also explicitly
define a constructor without arguments that replaces the standard
constructor.
For example, for the class Person, we could define the following
constructor without arguments:
public Person() { // constructor without arguments
name = "John Smith";
residence = null;
}
Note: It does not always make sense to define for a class a constructor
without arguments. For example, the definition of a constructor without
arguments for the class Person can certainly be questioned.
Next: Design methodology for a
Up: Unit 03
Previous: Overloading of constructors