Objective #1: Use a default constructor.
- A constructor is a block of code that
is executed when an object is instantiated (i.e. constructed). A constructor
usually initializes a
instantiated object by setting initial values to the object's properties
- A default
constructor is a constructor that has no parameters in its parentheses. For example, the following default constructor would be found in a Bug class:
myAge = 0;
In this example, a Bug object is given 10 rockets as ammunition and an x, y position of (0, 0). There can only be
one default constructor in a class. A default constructor often sets all of
the properties of an object to 0.
- In a client program, a programmer calls a default
constructor by typing new followed by the name of the class and a set
of empty parentheses. The statement
nemo = new Bug();
causes Java to execute the default constructor of the Bug class since the ...Bug()... portion of that statement refers to what
is called the default constructor of the Bug class.
- A class can have one or more constructors that
each have parameters in the parentheses. I call this an example of an
"other" constructor". For example:
public Bug(int bAge, int bX, int bY)
myAge = bAge;
myX = x;
myY = y;
is a constructor that has 3 parameters.
- In a client program, a programmer calls an "other" constructor by typing values into the parentheses as in:
nemo = new Bug(3, 50, 50);
The 3 parameters in the parentheses represent the age, the x-position, and the y-position of a Bug object, respectively.
- A class may have many "other" constructors
but it can only one default constructor. Each of the "other" constructors
must have a
different parameter list (aka signature). Either the number of parameters between two other constructors must be different
of the data
must be different. Including many other constructors in a class makes it
easier for client programmers to instantiate objects of the class.
- If no constructors are included in a class, Java will automatically
supply a default constructor behind-the-scenes. In this case, all int and double
properties will be initialized to zero by default. However, if at least
one "other" constructor with one or more parameters is implemented by
the programmer then Java will not automatically supply a default
- One constructor can call another constructor for efficiency. If you have
an "other" constructor that accepts the parameters age, x, and
y such as
public Bug(int age, int x, int y)
myAge = myAge;
myX = x;
myY = y;
then you can implement the Bug class's default constructor in this way....
this(10, 0, 0);
where the this keyword is used as a call statement that passes
3 values to the other constructor which accepts three int parameters.
- A constructor must have the exact same
name as the class to which it belongs.
- A parameter to a constructor or a method is
called an explicit parameter or an argument. The object (i.e. instance) itself that is
being manipulated by a method or constructor is referred to as the implicit
An implicit parameter can be referred to through the this keyword.
- Technically, constructors are not considered to be methods.
- String objects are exceptions
to the rule of using the new operator or even constructors in general. Either
of the statements below can be used to declare a String object variable,
String object, and immediately initialize it to the word "Smith"
String lastName = new String("Smith");
String lastName = "Smith";