In Java, a constructor is a special type of method that is used to initialize objects. It is automatically called when an object of a class is created. Constructors help in setting initial values for object attributes.
- A constructor has the same name as the class.
- It does not have a return type, not even
void
. - It is automatically invoked when an object is created.
- Constructors can have parameters to initialize objects with different values.
A default constructor is a constructor without parameters. If no constructor is defined in a class, Java automatically provides a default constructor.
// Defining a class
class Car {
// Constructor
Car() {
System.out.println("Car object is created!");
}
public static void main(String[] args) {
// Creating an object of Car class
Car myCar = new Car();
}
}
A parameterized constructor allows us to pass values while creating an object.
// Defining a class
class Car {
String brand;
int speed;
// Parameterized Constructor
Car(String b, int s) {
brand = b;
speed = s;
}
void display() {
System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
public static void main(String[] args) {
// Creating objects with different values
Car car1 = new Car("Tesla", 250);
Car car2 = new Car("BMW", 220);
// Displaying object details
car1.display();
car2.display();
}
}
Brand: Tesla
Speed: 250 km/h
Brand: BMW
Speed: 220 km/h
A copy constructor creates a new object by copying the values of an existing object.
class Car {
String brand;
int speed;
// Parameterized Constructor
Car(String b, int s) {
brand = b;
speed = s;
}
// Copy Constructor
Car(Car obj) {
brand = obj.brand;
speed = obj.speed;
}
void display() {
System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
public static void main(String[] args) {
Car car1 = new Car("Tesla", 250);
Car car2 = new Car(car1); // Copying car1's values into car2
car1.display();
car2.display();
}
}
Brand: Tesla
Speed: 250 km/h
Brand: Tesla
Speed: 250 km/h
Constructor overloading allows a class to have multiple constructors with different parameters.
class Car {
String brand;
int speed;
// Default Constructor
Car() {
brand = "Unknown";
speed = 0;
}
// Parameterized Constructor
Car(String b, int s) {
brand = b;
speed = s;
}
void display() {
System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
public static void main(String[] args) {
Car car1 = new Car(); // Calls default constructor
Car car2 = new Car("Audi", 240); // Calls parameterized constructor
car1.display();
car2.display();
}
}
Brand: Unknown
Speed: 0 km/h
Brand: Audi
Speed: 240 km/h
Constructors are essential in Java for initializing objects efficiently. We learned about:
- Default constructors (automatically created if no constructor is defined).
- Parameterized constructors (used to assign values while creating objects).
- Copy constructors (used to create a new object by copying values from another object).
- Constructor overloading (multiple constructors with different parameters).
Subscribe to our newsletter
Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.