Each object has a special method called a constructor. The constructor can be overloaded as necessary.
Typically, you will have a default constructor (sometimes called a no-arg constructor), where no parameters is passed to the constructor.
This special method is used to constructor an object when it is created. It is very similar to a method, but has a couple of minor changes. The generic view is below:
<access modifier> <Class Name>(<parameter list>) {
// constructor body
}
Notice that the access modifier, sometimes called the accessor, is the same, and the same data types apply. Typically you will have a public
modifier, since you want people to be able to create the object, but not always.
Instead of a method name, notice you have a class name. The constructor must be the same name as the class, which is the same name as the file, except for some specific instances which we are not going to worry about here. Remember class names, should be capitalized if you are following the Java naming convention.
The parameter list is the same as a method’s parameter list as is the body.
What’s Different Between a Constructor and a Method
The difference you might notice is that there is no return type. The object, of that class, is the data type being returned. It is the only type of data that a Constructor can return, so it never has to be listed. This, and the name having to be the Class name are two of the rules about constructors.
Overloading a Constructor
As mentioned before, constructors are usually overloaded. Every Class gets a default constructor through inheritance, which is something we’ll discuss later.
The default constructor is good for creating a basic instance of a class. However, we often want to pass data to the constructor to build a new one. This way the object is already configured as we’d want it. Consider a class called Circle
as shown below with the attributes shown as well. Notice how there are several different constructors which allow the Circle to be constructed based upon what is passed to the constructor.
public class Circle {
private double radius;
private double x;
private double y;
public Circle() {
radius = 1;
x = 0;
y = 0;
}
public Circle(double radius) {
// what if only one parameter is passed in - define it to be the radius
this();
this.radius = radius;
}
public Circle(double radius, double x, double y) {
// what if only three parameters are passed in
this();
this.radius = radius;
this.x = x;
this.y = y;
}
}
this()
Notice how in the overloaded methods, we call this()
. That is calling the default constructor, so all of your parameters are set, then you only need to set the parameters that change. This is a quick way to setup large complex objects which may have a lot of defaults.
Notice how I do it, even if I pass in three parameters, one for each of the class attributes. This way, if another attribute is added, it will be set in the default constructor, and I don’t have to go in and remember to set it in each of my constructors.
this.
If you notice, in each of the constructors with parameters, we pass in a parameter with the same name as the attribute. You cannot say x = x
, because that would be referring to the parameter variable. Instead, use the this.
to reference the instance variable/attribute. That way you can differentiate them.
Alternatively, you can use different names for the parameter names and the instance variable names if you’d like.
Calling a Constructor
Calling a constructor is a relatively simple process. Anytime you create an object, you are calling the constructor when you use the new
keyword.
Circle c1 = new Circle(); // calling default constructor
Circle c2 = new Circle(3); // constructor call with one parameter
Circle c3 = new Circle(3, 1, -5); // constructor call with three parameters
Notice that we’re calling the constructors that we created above. So we couldn’t call a constructor with 5, 6, or even just 2 parameters since those didn’t exist in our class.
Java Constructors was originally found on Access 2 Learn