Constructors
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
newly
instantiated object by setting initial values to the object's instance variables
(i.e. properties or fields).
- A constructor must have the exact same
name as the class to which it belongs.
- Technically, a constructor is not considered to be a method.
- There can only be
one default constructor in a class.
- Normally, a default constructor sets all of
the instance variables of an object to zero or null references.
- 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:
public Bug()
{
myAge = 0;
myX = 0;
myY = 0;
}
Objective #2: Use constructors with parameters.
- A class can have one or more constructors that each have different sets of parameters in their parentheses (i.e. parameter lists).
- I call this type of constructor with parameters an "other" constructor to differentiate it from the one and only default constructor that is usually found in a class.
- Here is an example of an "other" constructor":
public Bug(int age, int x, int y)
{
myAge = age;
myX = x;
myY = y;
}
is a constructor that accepts 3 parameters.
- 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 method 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 age, x, and y such as
public Bug(int age, int x, int y)
{
myAge = age;
myX = x;
myY = y;
}
then you can implement the Bug class's default constructor in this way....
public Bug()
{
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 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
parameter.
An implicit parameter can be referred to through the this keyword.
Objective #3: Call constructors from a client program.
- 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.
- String objects are exceptions
to the rule of using the new operator. 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";
- Integer and Double objects are exceptions
to the rule of using the new operator. Either
of the statements below can be used:
Integer num = new Integer(0);
or
Integer num = 0;
and
Double num = new Double(0.0);
or
Double num = 0.0;
- In a client program, a programmer calls an "other" constructor by typing values into the parentheses as in:
nemo = new Bug(3, 50, 50); // age, x position, y position
Objective #4: Use the keyword this when appropriate.
- I consider it to be good style (especially for beginners) to always name instance variables in a class with the prefix my and to drop the prefix my when naming parameters in constructors or modifier methods.
Example:
public class Bug
{
private int myAge;
public Bug(int age)
{
myAge = age;
}
public void setAge(int age)
{
myAge = age;
}
When the following statement executes in a client program
Bug flik = new Bug(3);
the object variable flik will have the desired age of 3.
However it is legal in Java to use the same name for a constructor or method's parameter as an instance variable. So the following version of the other constructor would compile without errors even though it is horribly confusing:
public class Bug
{
private int age;
public Bug(int age)
{
age = age;
}
public void setAge(int age)
{
age = age;
}
When the following statement executes in a client program
Bug flik = new Bug(3);
the object variable flik will have an age of zero! The statement age = age; will be interpreted by Java as setting the parameter age to 3 because the value of the incoming parameter age is 3. That is both sides of the assignment statement age = age; refer to the formal parameter named age and not the instance variable. Therefore the instance variable age will default to the value of zero.
The Java keyword this can be used to reduce this confusion and ambiguity as in this example
public class Bug
{
private int age;
public Bug(int age)
{
this.age = age;
}
public void setAge(int age)
{
this.age = age;
}
In this case, the statement
Bug flik = new Bug(3);
will cause the value 3 to be assigned to the instance variable age since this.age clarifies the situation.
It is best to avoid this potential confusion and possible logic errors in Java by using a naming convention like prefixing instance variables with my.