Classes and Objects in Java – Constructors
In creating objects of the type Fraction, we have used statements similar to the following:
Fraction f = new Fraction();
The parentheses in the expression Fraction() makes it look like a method, yet we never created
such a method in our class. In fact, it is a special type of method called a constructor method.
Although we didn't create this method, Java automatically creates a default constructor method. The
constructor method has the same identifier as the class (in this case, the word 'Function' is the
identifier for both the constructor and the class).
Constructor methods are instance methods used to initialize new objects at the time they are created.
The default constructor method will perform the following initializations:
1. All numeric fields are set to zero.
2. All boolean fields are set to false.
3. All reference fields (i.e., fields that refer to an object, rather than a primitive data type) are set
to null.
Note: The value of null is, in essence, 'nowhere'. By setting a reference field to null, we are
explicitly stating that it does not refer to any object. On the other than, an uninitialized
reference field could theoretically contain any value, including locations in memory occupied by
running programs. This is one of the many reasons why it is critical to always initialize all
variables to known starting values.
Redefining the Constructor Method
Java creates a constructor method by default for all classes. It is possible, however, to specify our
own constructor method, which will then replace the default constructor.
Suppose, in the class Fraction, we created the following constructor:
public Fraction (int n, int d)
{
[Link] = n;
[Link] = d;
}
Notice that this constructor method accepts the integer arguments n and d. We might create a new
instance of a Fraction object with
Fraction f = new Fraction(2, 3);
This would declare f to be of type Fraction, create an object of type Fraction in memory, set f to
2
refer to that object in memory, and then initialize the object to represent the fraction 3
.
Some notes about our first constructor method:
1. The identifier of the constructor is exactly the same as the identifier of the class.
2. Unlike every method we have seen so far, the constructor method does not specify a return
type (not even void).
3. The constructor is an instance method, allowing us to use the keyword this.
4. The call to the constructor requires the use of the keyword new.
5. The parameters were called n and d to avoid any potential conflicts with num and den (since
the use of the keyword this is optional).
28. Oct. 2010 Page 1 of 5
Classes and Objects in Java – Constructors
Overloading Constructors
Like other methods we have previously discussed, it is also possible to overload a constructor method.
This allows us to create multiple versions of the constructor method, each with its own signature and
each doing some different kind of initialization.
// constructor to initialize the new object to be
// equal to an existing object f
public Fraction (Fraction f)
{
[Link] = [Link];
[Link] = [Link];
}
Once we have created our own constructor method, the default constructor method no longer
operates. It is possible, however, that we wish to have a constructor method with no parameters, in
which case we will have to write our own.
In the case of the Fraction class, the default constructor method will initialize the numerator and
0
denominator to zero, creating the fraction 0 . It might be more logical to initialize the fraction to
0
something mathematically safer, such as 1 .
public Fraction()
{
[Link] = 0;
[Link] = 1;
}
Consider the results of the following code fragment:
Fraction p = new Fraction(3, 5);
Fraction q = new Fraction(p);
Fraction r = new Fraction();
p q r
Fraction Fraction Fraction
num 3 num 3 num 0
den 5 den 5 den 1
28. Oct. 2010 Page 2 of 5
Classes and Objects in Java – Constructors
Questions & Exercises
1. Fraction class.
(a) Extend the definition of the Fraction class that we have been developing to include the
constructors discussed in this section.
(b) Write a main method that first constructs two Fraction objects representing the fractions 5/7
and 3/8, and then creates two other Fraction objects whose values are the sum and product
of the original fractions. The method should output the numerator and denominator values for
each fraction. We will develop a more refined output of a Fraction object in the future. Use
the methods plus and times from the lesson on instance methods to help you complete this
task.
2. For the class Circle, as previously discussed:
(a) Write a constructor method that has no parameters. The method should construct a Circle
object with centre (0,0) and radius 1.
(b) Write a constructor method that has three parameters representing the coordinates of the
centre (x,y) and the radius, r, of the circle to be constructed.
(c) Write a constructor with a single parameter of type Circle. The method should construct a
new Circle object with the same field values as those of the circle specified by the
parameter.
28. Oct. 2010 Page 3 of 5
Classes and Objects in Java – Constructors
Solutions
1. (a) // This replaces the default constructor for the Fraction class
public Fraction()
{
[Link] = 0;
[Link] = 1;
}
/**
* This is another constructor for the Fraction class.
* It can be distinguished from other constructors by the
* parameter list.
*/
public Fraction(int n, int d)
{
[Link] = n;
[Link] = d;
}
/**
* Constructor to initialize the new object to be equal to
* an existing object f
*/
public Fraction (Fraction f)
{
[Link] = [Link];
[Link] = [Link];
}
(b) class TestFraction
{
public static void main (String[] args)
{
Fraction f1 = new Fraction(5, 7);
Fraction f2 = new Fraction(3, 8);
Fraction f3 = [Link](f2);
Fraction f4 = [Link](f2);
[Link]("Fraction 1: " + [Link] + "/" + [Link]);
[Link]("Fraction 2: " + [Link] + "/" + [Link]);
[Link]("Fraction 3: " + [Link] + "/" + [Link]);
[Link]("Fraction 4: " + [Link] + "/" + [Link]);
}
}
2. (a) public Circle()
{
// initialize instance variables
x = 0;
y = 0;
r = 1;
}
28. Oct. 2010 Page 4 of 5
Classes and Objects in Java – Constructors
(b) public Circle(double x, double y, double r)
{
// initialise instance variables
this.x = x;
this.y = y;
this.r = r;
}
(c) public Circle(Circle copy)
{
this.x = copy.x;
this.y = copy.y;
this.r = copy.r;
}
28. Oct. 2010 Page 5 of 5