Constructors in Java

Constructors are a fundamental concept in Java, used to initialize objects. When you create an instance of a class, a constructor is automatically called. This special method sets the initial state of the object, assigning values to its fields and preparing it for use. Unlike regular methods, constructors don’t have a return type, not even void.

Key Features of Constructors

  1. Name Matching Class: A constructor must have the same name as the class. For instance, if the class is Car, the constructor will also be named Car.
  2. No Return Type: Constructors do not return any value, not even void. Their primary function is to set up the object.
  3. Automatic Invocation: When you use the new keyword to create an object, Java automatically calls the class’s constructor.
  4. Types of Constructors: Java provides two types of constructors:
    • Default Constructor
    • Parameterized Constructor

1. Default Constructor A default constructor has no parameters. If you don’t explicitly define any constructor in your class, Java will automatically create a default constructor. Here’s a simple example:

public class Car {
// Default constructor
public Car() {
System.out.println("Car object created.");
}
public static void main(String[] args) {
Car myCar = new Car(); // Calls the default constructor
}
}

In the example above, creating a Car object calls the default constructor, which prints “Car object created.”

2. Parameterized Constructor A parameterized constructor allows you to pass arguments when creating an object, providing flexibility to initialize fields with specific values. Here’s how it works:

public class Car {
String model;
int year;
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}

public static void main(String[] args) {
Car myCar = new Car(“Toyota”, 2021);
System.out.println(“Model: “ + myCar.model);
System.out.println(“Year: “ + myCar.year);
}
}

In this case, the constructor takes two arguments: model and year. The this keyword helps differentiate between the class fields and the parameters.

Why Use Constructors?

  1. Initialization: Constructors simplify initializing objects by setting default or custom values directly during object creation.
  2. Mandatory Setup: They ensure that an object is properly set up before it’s used, reducing errors and making the code more predictable.
  3. Overloading Flexibility: You can overload constructors by defining multiple constructors with different parameter lists, which provides flexibility when creating objects.

Constructor Overloading Just like regular methods, constructors can be overloaded by creating multiple constructors with different parameters. For example:

public class Car {
String model;
int year;
// Default constructor
public Car() {
this.model = “Unknown”;
this.year = 0;
}

// Parameterized constructor
public Car(String model) {
this.model = model;
this.year = 2023; // default year
}

// Another parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}

public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car(“Honda”);
Car car3 = new Car(“Ford”, 2020);

System.out.println(car1.model + ” “ + car1.year); // Unknown 0
System.out.println(car2.model + ” “ + car2.year); // Honda 2023
System.out.println(car3.model + ” “ + car3.year); // Ford 2020
}
}

In this example, you can see how constructor overloading lets you create objects with varying levels of detail.

Summary

  • Constructors in Java are special methods used to initialize new objects.
  • They have no return type and share the same name as the class.
  • You can have default constructors (no parameters) or parameterized constructors (with parameters).
  • Constructors ensure that every object starts with a valid, predictable state.

Using constructors effectively makes your code clean, efficient, and easy to understand. Whether you’re building small or complex applications, understanding constructors is a fundamental step toward mastering Java.

public class Methods_Constructors {

// void – a method that RETURNS NOTHING

// If you add void, then the constructor turns into a method

// A method can have a return type, but a constructor cannot have any method

// A method in Java is like a function; it’s elements of a class designed to perform actions

// Each method must have a return type. If we want the method to return nothing, we should

// write the word void – emptiness.

int sum(int a, int b, int c) {

// int – return type, sum – method name

// (int a, int b, int c) – method parameters

 

int result = a + b + c;

return result;

// return MUST always return the same data type

// as specified in the return type

}

// This is how a method is created

int averageNumber(int a1, int b1, int c1) {

int result2 = sum(a1, b1, c1) / 3;

return result2;

// calling the sum method inside the new averageNumber method

}

}

Scroll to Top