Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 18

Constructors in Java

In this lesson: Types of constructors in Java — default, parameterized, overloading and this() — rules and examples with output. In Hindi & English.

What is a constructor

A constructor is a special method that runs automatically when you create an object with new. Its job is to set up the object's starting values. It has the same name as the class and no return type.

💡 A constructor runs once, at object creation, to initialise the object's data.

The default constructor

class Student {
    String name;
    Student() {                 // default constructor
        name = "Unknown";
    }
}
// use:
Student s = new Student();
System.out.println(s.name);
Unknown

A no-argument constructor sets default values. If you write no constructor at all, Java provides an invisible empty one so new still works.

Parameterised constructor

class Student {
    String name;
    int marks;
    Student(String n, int m) {   // parameterised
        name = n;
        marks = m;
    }
}
// use:
Student s = new Student("Aman", 75);
System.out.println(s.name + " " + s.marks);
Aman 75

A parameterised constructor takes values so you can set up an object with custom data at creation. This is cleaner than creating an object and setting each field afterwards.

Constructor overloading

class Box {
    int w, h;
    Box() { w = 1; h = 1; }            // default size
    Box(int side) { w = side; h = side; } // square
    Box(int w, int h) { this.w = w; this.h = h; } // custom
}

Having several constructors with different parameters is overloading. Java picks the one matching your arguments — new Box(), new Box(5), or new Box(4, 6).

Calling this()

class Box {
    int w, h;
    Box() {
        this(1, 1);            // call the other constructor
    }
    Box(int w, int h) {
        this.w = w; this.h = h;
    }
}

this() calls another constructor in the same class, so one can reuse another's setup instead of repeating code. It must be the first line in the constructor.

Constructor vs method

PointConstructorMethod
NameSame as classAny name
Return typeNoneRequired (or void)
When it runsAutomatically on newWhen you call it
PurposeSet up the objectDo a task

Common mistakes

  • Adding a return type. A constructor has none — adding one makes it a method.
  • Wrong name. A constructor must exactly match the class name.
  • Losing the default. If you add any constructor, the free default one is gone — add it back if needed.
  • this() not first. A this() call must be the first statement in the constructor.

Summary

  • A constructor runs automatically when an object is created.
  • It has the class's name and no return type.
  • Default takes no arguments; parameterised takes values.
  • Overloading provides several constructors with different parameters.
  • this() calls another constructor to reuse its setup.

Next, learn this and super, or reuse code with inheritance.

Frequently Asked Questions

What is a constructor in Java?
A constructor is a special method that runs automatically when an object is created with new. It sets up the object's starting values. It has the same name as the class and no return type.
What is the difference between a default and a parameterised constructor?
A default constructor takes no arguments and sets default values. A parameterised constructor takes arguments so you can give an object custom values at creation, like new Student("Aman", 18).
What is constructor overloading?
Constructor overloading means having several constructors in the same class with different parameter lists. Java picks the right one based on the arguments you pass, giving flexible ways to create objects.
Why does a constructor have no return type?
A constructor's job is to build and initialise the object, not to return a value, so it has no return type — not even void. If you add a return type, it becomes a normal method, not a constructor.
What does this() do in a constructor?
this() calls another constructor in the same class. It lets one constructor reuse another's setup code, avoiding repetition. It must be the first statement in the constructor.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicInheritance Your progress: 18 / 61 (30%) · Lesson 19 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free