DEV Community

MOHAMED ABDULLAH
MOHAMED ABDULLAH

Posted on

Constructor

Constructors are special methods in Java used to initialize objects. Unlike regular methods, constructors have the same name as the class and do not have a return type.

A constructor is automatically called when an object is created. It sets initial values for object attributes.

Key Points:
Constructor name = class name

No return type (not even void)

Called automatically when an object is created

Can be default, no-argument, or parameterized

Default Constructor (Provided by Compiler)
Explanation:
If you don’t write any constructor, Java automatically adds a no-argument constructor called the default constructor.

It does nothing but allows object creation.

When to use:
When no special initialization is needed for the object.

  1. Default Constructor (Provided by Compiler) Structure: You don’t need to write this. If no constructor is defined, Java will create this for you behind the scenes.

structure:

class MyClass {
// No constructor defined, so compiler adds:
// MyClass() { }
}

Used like this:

MyClass obj = new MyClass(); // compiler-created default constructor

Example:

class Book {
void show() {
System.out.println("This is a book.");
}
}

public class Main {
public static void main(String[] args) {
Book b1 = new Book(); // default constructor is used
b1.show();
}
}

Output:
This is a book.

  1. No-Argument Constructor (User-defined) Explanation: This is a constructor you write yourself with no parameters.

You can add custom logic inside it (like printing or initializing values).

When to use:
When you want to execute some logic when the object is created, but no input is needed.
Structure:

class MyClass {
MyClass() {
// constructor body
System.out.println("No-arg constructor called");
}
}
Used like this:

MyClass obj = new MyClass(); // calls no-arg constructor

Example:

class Car {
Car() {
System.out.println("Car is ready!");
}
}

public class Main {
public static void main(String[] args) {
Car c1 = new Car(); // no-arg constructor is called
}
}

Output:

Car is ready!

  1. Parameterized Constructor Explanation: This constructor accepts parameters so you can initialize object variables during object creation.

Useful for setting different values for different objects.

When to use:
When you want to assign values to object fields at the time of creation.

Parameterized Constructor
Structure:

class MyClass {
int x;

MyClass(int value) {
    x = value;
}
Enter fullscreen mode Exit fullscreen mode

}
Used like this:

MyClass obj = new MyClass(10); // calls parameterized constructor

Example:

class Student {
String name;
int age;

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

void display() {
    System.out.println("Name: " + name + ", Age: " + age);
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Student s1 = new Student("Abdullah", 22);
s1.display();
}
}

Output:

Name: Abdullah, Age: 22

reference link 1 :
https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

reference link 2:
https://docs.oracle.com/javase/specs/jls/se17/html/jls-8.html#jls-8.8

reference link 3 :
https://www.geeksforgeeks.org/constructors-in-java/

Top comments (0)