Java Constructors
In this article, we cover constructors and their use in classes
Table of contents
One of the main functions of classes is to be able to create objects from the class. This is where constructors come in. As the name suggests, constructors define how to construct objects from the class.
Constructors don't need a return type.
It is good practice to define them as the first method of the class.
Note: A constructor must have the same name as the class.
There are 2 types of constructors:
Default constructors
Parameterized constructors
Default constructors
Default constructors simply define the default values for objects created from the class. Hence, whenever you create instances from the class, the values defined in the default constructors will be the default values.
Example:
class Car {
private String manufacturer;
private int engineSize;
private int powerSupply;
// Default constructor
public Car() {
// We define the default values for manufacturer, engineSize, and powerSupply
manufacturer = "Toyota";
engineSize = 3000;
powerSupply = 450;
}
// A simple print function
public void printCarDetails(){
System.out.println("Manufacturer: " + manufacturer);
System.out.println("Engine Size: " + engineSize);
System.out.println("Power Supply: " + powerSupply);
}
}
class CarDemo {
public static void main(String args[]) {
// Call the Car constructor to create its object
Car car1 = new Car(); // Object created with default values
car1.printCarDetails();
}
}
Note: Notice that when we created a Car
object in, we don’t treat the constructor as a method and write this:
car.Car();
We create the object just like we create any other object:
Car car1 = new Car();
In addition, even if we don’t define a default constructor, the JVM will call a default constructor and set data members to null
or 0
.
Parameterized constructor
In a parameterized constructor, we pass arguments to the constructor and set them as the values of our member variables. It's basically overloading the default constructor with our preferred values for the member variables.
class Car {
private String manufacturer;
private int engineSize;
private int powerSupply;
// Default constructor
public Car() {
// We define the default values for manufacturer, engineSize, and powerSupply
manufacturer = "Toyota";
engineSize = 3000;
powerSupply = 450;
}
// Parameterized constructor
public Car(String make, int eSize, int power){
// The arguments are used as values
manufacturer = make;
engineSize = eSize;
powerSupply = power;
}
// A simple print function
public void printCarDetails(){
System.out.println("Manufacturer: " + manufacturer);
System.out.println("Engine Size: " + engineSize);
System.out.println("Power Supply: " + powerSupply);
}
}
class CarDemo {
public static void main(String args[]) {
// Call the Car constructor to create its object
Car car1 = new Car("Honda", 3300, 250); // Object created with default values
car1.printCarDetails();
}
}
this
keyword
The this
keyword exists for every class. It refers to the class object itself. We use this
when we have an argument that has the same name as a member variable. this.memberVariable
specifies that we are accessing the memberName
variable of the particular class.
class Car {
private String manufacturer;
private int engineSize;
private int powerSupply;
// Default constructor
public Car() {
// We define the default values for manufacturer, engineSize, and powerSupply
this.manufacturer = "Toyota";
this.engineSize = 3000;
this.powerSupply = 450;
}
// Parameterized constructor
public Car(String manufacturer, int engineSize, int powerSupply){
// The arguments are used as values
this.manufacturer = manufacturer;
this.engineSize = engineSize;
this.powerSupply = powerSupply;
}
// A simple print function
public void printCarDetails(){
System.out.println("Manufacturer: " + manufacturer);
System.out.println("Engine Size: " + engineSize);
System.out.println("Power Supply: " + powerSupply);
}
}
class CarDemo {
public static void main(String args[]) {
// Call the Car constructor to create its object
Car car1 = new Car("Honda", 3300, 250); // Object created with default values
car1.printCarDetails();
}
}
We can also use it when calling a constructor from another constructor. The this
keyword followed by parentheses means that another constructor in the same Java class is being called.
Example:
class Car {
private String manufacturer;
private int engineSize;
private int powerSupply;
// Default constructor
public Car() {
// We define the default values for manufacturer, engineSize, and powerSupply
this.manufacturer = "Toyota";
this.engineSize = 3000;
this.powerSupply = 450;
}
// Parameterized constructor
public Car(String manufacturer, int engineSize, int powerSupply){
// The arguments are used as values
this.manufacturer = manufacturer;
this.engineSize = engineSize;
this.powerSupply = powerSupply;
}
// Parameterized constructor
public Car(String manufacturer, int engineSize, int powerSupply, int fuelCapacity){
this(manufacturer, engineSize, powerSupply); // calling the constructor
this.fuelCapacity = fuelCapacity;
}
// A simple print function
public void printCarDetails(){
System.out.println("Manufacturer: " + manufacturer);
System.out.println("Engine Size: " + engineSize);
System.out.println("Power Supply: " + powerSupply);
}
}
class CarDemo {
public static void main(String args[]) {
// Call the Car constructor to create its object
Car car1 = new Car("Honda", 3300, 250); // Object created with default values
car1.printCarDetails();
}
}
Conclusion
Constructors are very important when creating objects. All classes need them to create instances of the class.