Java
TOPIC 36 – CONSTRUCTORS I
LESSON NOTE
INTRO TO CONSTRUCTORS
So far, the classes that we have used have consisted strictly of a
grouping of instance variables (also known as data fields). We will now look at a second component of
classes – constructors.
A constructor is a special method that is always called when you
create an object. Its responsibility
is to initialize all the instance variables.
All instance variables are accessible from inside constructors.
GENERAL CONSTRUCTOR FORM
The general form of a constructor is as follows:
public ClassName(argument list)
{
//statements that
initialize all data fields
}
You
will notice that constructors have a similar organization as functions (aka
static methods) from our last unit.
DETAIL
#1 – NOT STATIC
Constructors are not static. This
means that they have access to the data fields (instance variables) of the
class.
Notice that we do not put the word static in the prototype.
DETAIL
#2 – NO RETURN-TYPE
Constructors have no return type. You
do not put anything in the prototype.
So no return type and no static.
DETAIL
#3 – CONSTRUCTOR NAME
The name of the constructor is simply the class name.
Therefore, a constructor for a Point class would be named Point(…) and
a constructor for a Rectangle class would be named Rectangle(…).
DETAIL #4 – ARGUMENT LIST
The argument list works in the exact same way as it does for static methods
(functions).
DETAIL
#5 – PURPOSE OF CONSTRUCTOR
The purpose of the constructor is simply to initialize each data field
(instance variable).
Before looking at more details, we will look at an example of code with
a constructor in it. Don't hesitate to
re-read the above details after analyzing the code below.
EXAMPLE – ADDING A CONSTRUCTOR TO A SIMPLE CLASS
Consider the following Point class:
public
class Point
{
public double x;
public double y;
}
We will now add a simple constructor to it.
We place the constructor beneath the data fields.
public Point(double
xvalue, double yvalue)
{
x = xvalue; //setting x
data field
y = yvalue; //setting y
data field
}
The new Point class now looks like this:
public
class Point
{
public double x;
public double y;
public
Point(double xvalue, double yvalue)
{
x = xvalue; //setting x data field
y
= yvalue; //setting y
data field
}
}
The above constructor gets two values as parameters called xvalue and yvalue. These are local variables that will cease
to exist as soon as the constructor is done executing.
The constructor uses the parameter values to give values to the data fields
called x and y. The data fields
continue to exist and hold their value after the constructor is done
executing.
|
USING
A CLASS WITH A CONSTRUCTOR
To make use of a class with a constructor in it, we simply need to
create an object of that class and match the argument list in the
constructor's prototype.
In general, it will look like this:
ClassName obj = new ClassName(argument
list);
Of course, an example will be clearer.
EXAMPLE – USING A
CLASS WITH A CONSTRUCTOR IN IT
Consider
the class below that we created in the previous example.
public
class Point
{
public double x;
public double y;
public
Point(double xvalue, double yvalue)
{
x = xvalue; //setting x data field
y = yvalue; //setting y data field
}
}
The
following line of code will create a Point object.
Point z = new Point(4.2, 5.4);
Notice that the argument list contains the two doubles 4.2 and 5.4. This matches the argument list in the
constructor. So xvalue
will get the value 4.2. And yvalue will get 5.4.
Then, inside the constructor, x will be given 4.2 and y will get 5.4.
The following code will create a program that will create five different
Point objects and output their content to screen:
public
class FivePoints
{
public static void main(String[] args)
{
Point a = new Point(3.4, 5,2);
Point b = new Point(5.0,
1.2);
Point c = new Point(0.4,
31.5);
Point d = new Point(-5.0, 3.3);
Point e = new Point(9.6, 7.3);
System.out.println("(" + a.x
+ ", " + a.y + ")");
System.out.println("(" + b.x
+ ", " + b.y + ")");
System.out.println("(" + c.x
+ ", " + c.y + ")");
System.out.println("(" + d.x
+ ", " + d.y + ")");
System.out.println("(" + e.x
+ ", " + e.y + ")");
}
}
|
OBJECTS IN
MEMORY
An object in memory is a little more complex than a simple
variable. The object name is
associated to a memory reference where the data fields are stored in memory.
EXAMPLE OF
OBJECTS IN MEMORY
Consider the following statement:
Point p =
new Point(2.5, 6.1);
This will give the x data field a value of 2.5 and the y data field a
value of 6.1. In memory, the object
will look like this:
Similarly, we can create a Point object named q with coordinates (2.1,
-9.2) by using:
Point q = new Point(2.1,
-9.2);
The above object would look like this in memory:
After a Point object is created, we can still
directly access the data fields by using:
objectName.dataFieldName
For example, we could do:
p.x = 5.0; //change the x data field of the p
object to 5.0
After the above statement, the object p would look
like this:
|