I wish creating objects was easier (aka constructors)
Let’s say that the user wants to create a Rectangle
whose width
is 5
and height
is 8. Following code achieves this,
1
2
3
Rectangle r = new Rectangle();
r.width = 5;
r.height = 8;
However, it would be really nice if one could pass the values for the instance variables in the instantiation statement itself, as,
1
Rectangle r = new Rectangle(5, 8);
This is done through constructors
.
Constructors
-
A constructor is a method defined in the class.
-
A constructor must have the same name as the class.
-
A constructor has no return type (not even void).
-
There may be multiple constructors, each distinguished by its parameter list. Thus, we may have one constructor with no parameters, and another with one
int
parameter. -
A suitable constructor is automatically called during instantiation based on number of parameters passed. If an appropriate constructor is not found, a compilation error is generated.
Example - Constructors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Rectangle {
public double width, height;
public Rectangle() { //default constructor
width = 1;
height = 1;
}
//parameterized constructor for a square
public Rectangle(double side) {
width = side;
height = side;
}
//parameterized constructor - generic
public Rectangle(double w, double h) {
width = w;
height = h;
}
//rest of the code
}
Default constructor
It should be noted that a default constructor (without any parameters) is pre-defined for you by Java and that’s why you can instantiate objects without defining it yourself.
1
Rectange r = new Rectangle();
The default constructor assigns the default values for the appropriate data types to the instance variables. However, once you define a parameterized constructor, the built-in default constructor is taken away by Java. Thus, if you want to construct an object with default initial values for the instance variables, you need to re-define that!
Defining the default constructor
Let’s say the default Rectangle
instance should be of unit length. We
can define the default constructor as,
1
2
3
4
public Rectangle() {
width = 1;
height = 1;
}
Add a default constructor to the class
Circle
that assigns the value 1.0 to instance variableradius
.SOLUTION
1 2 3 4 //default constructor public Circle() { radius = 1; }
Add a parameterized constructor to the class
Circle
that assigns the value of the parameter passed to instance variableradius
.SOLUTION
1 2 3 4 //parameterized constructor public Circle(double r) { radius = r; }