Constructors
Estimated time to read: 2 minutes
Overview¶
A constructor is a special type of method that Java uses to initialise instances of a class.
- The constructor has no return value, not even void!
- It mist have the same name as the class it initialises
- The instance knows its own type, knows what methods are available, knows about its attributes. It's just that the attributes, by default, are essentially all 0s or whatever 0 means to that particular type.
You may not be required to provide a constructor for your classes.
- If you do not supply a constructor, Java will automatically initialise all the attributes to preset (low/0) values.
- If you are providing any constructor at all that does not take arguments, this must be set explicitly. Else Java will use the default behaviour above.
Creating Constructors¶
A new
operation constructor call returns a reference to the newly created object that the constructor initialises.
A class can have more than one constructor.
However, since the constructors all have the same name as the class, we can only distinguish constructors by their signatures.
Therefore, you can't have two constructors with the same signature, as shown in the example below. Car (int newSpeed)
and Car (int gasLevel)
can not co-exist in this class.
Example¶
public class Car {
private int speed;
private int gasoline;
private boolean engineState;
Car(){} //No arg constructor
Car(int newSpeed){ //Single arg constructor
speed = newSpeed;
}
Car(int newSpeed, boolean en){ // Multi arg constructor
speed = newSpeed;
engineState - en;
}
Car(int gasLevel){ //ERROR - Single arg constructor, but shares the same signature as the first constructor above
gasoline - gasLevel;
}
}
Calling a Constructor¶
A constructor is called when the new
operator is used to create an object.
In the example below, the class Car
can create some Car
objects and references in its main()
method.
public static void main(String[] args){
Car racerX = newCar();
Car racerY = newCar(45);
System.out.println("Speed for racerX is " + racerX.getSpeed());
System.out.println("Speed for racerY is " + racerY.getSpeed());
}
The output of the above code would be:
Multiple Constructors¶
It is possible for one constructor to call another constructor in the same class using the this()
statement. this()
acts as a place holder for calling another constructor of the same class. This reduces the potential for code to be duplicated between constructors.
Example of Multiple Constructors¶
public class Car{
public Car(){
}
public Car(String name){
this(name, 0, 0, false);
}
public Car(String name, int speed, int fuel, boolean running){
setName(name);
setSpeed(speed);
setGasoline(fuel);
setRunning(running);
}
...
public static void main(String [] args){
...
Car mach5 = new Car("Mach V", 250, 21, True);
...
}
}
In Example Two, if a Car object is created with just a name, then the second constructor is called. This constructor then creates an object with the name specified in the arguments, whilst also setting some defaults for the third constructor. In this case, setting speed and int to 0 and the running boolean to false.