Think of this as Level 1 = remember, Level 2 = understand, Level 3 = master.
A constructor is a special member of a class used mainly to initialize an object when it is created.
1. Constructor name = Class name
2. No return type
3. Invoked during object creation
Example:
class Student {
Student() {
System.out.println("Constructor executed");
}
public static void main(String[] args) {
Student s = new Student();
}
}Output:
Constructor executed
| Constructor | Method |
|---|---|
| Same name as class | Can have any valid name |
| No return type | Has return type or void |
| Used mainly for initialization | Performs an operation |
| Invoked as part of object creation | Usually called explicitly |
| Can be overloaded | Can be overloaded |
Student() // Constructor
void Student() // Methodclass Student {
int id;
String name;
public static void main(String[] args) {
Student s = new Student();
s.id = 101;
s.name = "Ravi";
System.out.println(s.id + " " + s.name);
}
}Flow:
Create object
↓
Assign id
↓
Assign name
↓
Print
class Student {
int id;
String name;
Student(int i, String n) {
id = i;
name = n;
}
public static void main(String[] args) {
Student s = new Student(101, "Ravi");
System.out.println(s.id + " " + s.name);
}
}Flow:
new Student(101, "Ravi")
↓
Constructor receives values
↓
id = 101
name = Ravi
A constructor that receives parameters:
Student(int id, String name)Example:
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
}Call:
Student s = new Student(101, "Ravi");A constructor with no parameters:
class Student {
int id;
String name;
Student() {
id = 101;
name = "Ravi";
}
}Call:
Student s = new Student();Output values:
101 Ravi
this refers to the current object.
Consider:
class Student {
int id;
Student(int id) {
this.id = id;
}
}Here there are two ids:
this.id → instance variable
id → constructor parameter
Therefore:
this.id = id;means:
object's id = parameter id
Without this:
class Student {
int id;
Student(int id) {
id = id;
}
public static void main(String[] args) {
Student s = new Student(101);
System.out.println(s.id);
}
}Output:
0
Why?
The constructor parameter id shadows the instance variable id.
this.id = id;| Instance Variable | Local Variable |
|---|---|
| Declared inside class, outside methods/constructors | Declared inside method/constructor/block |
| Belongs to object | Belongs to local execution scope |
| Gets default value | Does not get an automatic usable default value |
Example: int id; |
Example: int x = 10; |
Can be accessed using this |
Cannot be accessed using this as a local variable |
Example:
class Student {
int id; // Instance variable
Student(int value) {
int x = 10; // Local variable
this.id = value;
}
}Instance variables automatically get default values:
int → 0
double → 0.0
boolean → false
reference → null
But:
void display() {
int x;
System.out.println(x); // ❌
}A local variable must be assigned before it is read.
A class can have multiple constructors with different parameter lists.
class Student {
int id;
String name;
Student() {
id = 0;
name = "Unknown";
}
Student(int id, String name) {
this.id = id;
this.name = name;
}
}Now:
Student s1 = new Student();
Student s2 = new Student(101, "Ravi");Java selects the appropriate constructor based on the arguments.
Student()
Student(int)
Student(int, String)
Student(String, int)Changing only return type doesn't apply because constructors don't have return types.
Also, these cannot be distinguished merely by modifiers:
public Student(int x)
private Student(int x)They have the same parameter list and cannot coexist as separate constructors.
If you write no constructor at all:
class Student {
}Java provides a compiler-generated default constructor.
But if you write:
class Student {
Student(int id) {
}
}then:
Student s = new Student();❌ Error.
Why?
Because the compiler no longer automatically supplies a no-argument default constructor.
If you want both:
class Student {
Student() {
}
Student(int id) {
}
}Don't confuse:
this
with:
this()
Current object:
this.idCalls another constructor in the same class:
this(101, "Ravi");Example:
class Student {
int id;
String name;
Student() {
this(101, "Ravi");
}
Student(int id, String name) {
this.id = id;
this.name = name;
}
}Flow:
Student()
↓
this(101, "Ravi")
↓
Student(int, String)
↓
Initialize fields
this(...) must be the first statement in the constructor.
super() invokes a superclass constructor.
class Animal {
Animal() {
System.out.println("Animal");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog");
}
}Output:
Animal
Dog
Remember:
this() → same class constructor
super() → superclass constructor
Constructors:
❌ Cannot be inherited
❌ Cannot be overridden
✅ Can be overloaded
But when a subclass object is created, a superclass constructor is invoked as part of object construction.
✅ Yes.
class Demo {
private Demo() {
System.out.println("Constructor");
}
}Useful when you want to control object creation.
❌ No.
static Demo() { } // ❌❌ No.
final Demo() { } // ❌❌ No.
abstract Demo() { } // ❌❌ No.
This:
void Student() {
}is a method, not a constructor.
❌ No.
Invalid:
s.Student();Correct:
new Student();or constructor chaining:
this();
super();Yes.
class Student {
int id;
Student(int id) {
this.id = id;
}
public static void main(String[] args) {
System.out.println(new Student(101).id);
}
}Output:
101
There is no:
Student slocal reference variable.
class Student {
int id;
String name;
int age;
// No-argument constructor
Student() {
this(0, "Unknown", 0);
}
// Parameterized constructor
Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
void display() {
System.out.println(id + " " + name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student(101, "Ravi", 20);
s1.display();
s2.display();
}
}Output:
0 Unknown 0
101 Ravi 20
CONSTRUCTOR
│
┌──────────────┼──────────────┐
↓ ↓ ↓
BASIC UNDERSTAND MASTER
│ │ │
Same class Parameterized Overloading
No return type No-argument this()
Object creation Shadowing super()
Initialization this Inheritance
Default constructor
| Question | Answer |
|---|---|
| Same name as class? | ✅ Yes |
| Return type? | ❌ None |
Can it have void? |
❌ No |
| Can it be overloaded? | ✅ Yes |
| Can it be overridden? | ❌ No |
| Can it be inherited? | ❌ No |
| Can it be private? | ✅ Yes |
| Can it be static? | ❌ No |
Can this be used? |
✅ Yes |
Can this() call another constructor? |
✅ Yes |
Constructor = special class member + same class name + no return type + object initialization.
No-argument
Parameterized
this
this()
super()
constructor overloading
default constructor
shadowing
inheritance
When
newcreates an object, the appropriate constructor is invoked to initialize that object;thisidentifies the current object, whilethis()andsuper()are used for constructor chaining.