DEV Community

Gowtham
Gowtham

Posted on

Java - Constructor & Constructor Overloading

1.Constructor in Java:

A constructor is a special member of a class that is used to initialize objects.

A constructor is automatically called when an object is created using the new keyword.

Important Features of Constructors

  • The constructor name must be the same as the class name.
  • A constructor does not have a return type, not even void.
  • It is called automatically when an object is created.
  • Constructors can have parameters.
  • Constructors can be overloaded.

Example:

`class Student {

Student() {
    System.out.println("Constructor Called!");
}

public static void main(String[] args) {
    Student s1 = new Student();
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

Constructor Called!

Explanation:

  • Student is the class name.
  • Student() is the constructor.
  • new Student() creates an object.
  • The constructor is automatically executed when the object is created.

2.Types of Constructors in Java

Java constructors are commonly explained using two categories:

1.Default Constructor

If we do not write any constructor in a class, the Java compiler provides a default constructor.

It initializes instance variables with their default values.

Example:

`class Student {
int age;
String name;

public static void main(String[] args) {
    Student s1 = new Student();

    System.out.println(s1.age);
    System.out.println(s1.name);
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

0
null

Here, Java provides a default constructor because we did not define any constructor.

Note: If we write our own constructor, the compiler does not automatically provide a default constructor.

2.No-Argument Constructor

A no-argument constructor is a constructor written by the programmer that does not accept any parameters.

Example:

`class Student {

Student() {
    System.out.println("Welcome to Java!");
}

public static void main(String[] args) {
    Student s1 = new Student();
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

Welcome to Java!

Here, Student() is a programmer-defined no-argument constructor.

3.Parameterized Constructor

A parameterized constructor accepts arguments to initialize object values.

Example:

`class Student {
String name;
int age;

Student(String n, int a) {
    name = n;
    age = a;
}

void display() {
    System.out.println(name);
    System.out.println(age);
}

public static void main(String[] args) {
    Student s1 = new Student("Gowtham", 20);
    s1.display();
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

Gowtham
20

Explanation:

  • Student(String n, int a) is a parameterized constructor.
  • name = n assigns the name.
  • age = a assigns the age.
  • new Student("Gowtham", 20) creates an object and passes values to the constructor.

3.Constructor Overloading in Java:

Constructor overloading means creating multiple constructors in the same class with different parameter lists.

It allows us to initialize objects in different ways.

Example:

`class Student {

String name;
int age;

Student() {
    name = "Unknown";
    age = 0;
}

Student(String n) {
    name = n;
    age = 18;
}

Student(String n, int a) {
    name = n;
    age = a;
}

void display() {
    System.out.println(name + " " + age);
}

public static void main(String[] args) {

    Student s1 = new Student();
    Student s2 = new Student("Gowtham");
    Student s3 = new Student("Arun", 21);

    s1.display();
    s2.display();
    s3.display();
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

Unknown 0
Gowtham 18
Arun 21

Explanation:

  • In this example, we have created three constructors:
  • Student() – No-argument constructor.
  • Student(String n) – Constructor with one parameter.
  • Student(String n, int a) – Constructor with two parameters.
  • Java automatically selects the matching constructor based on the arguments passed when creating an object.

6.Advantages of Constructor Overloading

Flexibility: Create objects with different initial values.

Code reusability: Avoid repeating initialization logic.

Easy to use: Choose a constructor based on the available information.

Better organization: Keep object initialization inside the class.

Top comments (0)