Constructors
Objective #1: Use constructors.
- A constructor is a block of code that
is executed when an object is instantiated (i.e. constructed). A constructor
usually initializes a
newly
instantiated object by setting initial values to the object's properties
(i.e. fields).
- A default
constructor is a constructor that has no parameters in its parentheses. For example, the following default constructor would be found in a Tank class:
public Tank()
{
myAmmo = 10;
myX = 0;
myY = 0;
}
In this example, a Tank 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
sherman = new Tank();
causes Java to execute the default constructor of the Tank class since the ...Tank()... portion of that statement refers to what
is called the default constructor of the Tank 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 Tank(int ammo, int x, int y)
{
myAmmo = ammo;
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:
sherman = new Tank(15, 50, 50);
The 3 parameters in the parentheses represent the amount of ammunition, the x-position, and the y-position of a Tank 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
or
the order
of the data
types
of
the parameters
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 constructor.
- One constructor can call another constructor for efficiency. If you have
an other constructor that accepts the parameters ammo, x, and
y such as
public Tank(int ammo, int x, int y)
{
myAmmo = ammo;
myX = x;
myY = y;
}
then you can implement the Tank class's default constructor in this way....
public Tank()
{
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. The object (i.e. instance) itself that is
being manipulated by a method or constructor is referred to as the implicit
parameter.
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,
instantiate
an actual
String object, and immediately initialize it to the word "Smith"
String lastName = new String("Smith");
or
String lastName = "Smith";