Skip to main content

Constructors

βœ” Definition​

  • Constructors are special methods whose name is the same as the class name
  • Constructors do not have a return type (not even void)
  • Constructors are invoked automatically by JVM at the time of object creation
  • Constructors are mainly used to initialize instance variables

Lab329.java​

class Lab329 {

public static void main(String[] args) {
Student stu1 = new Student();
stu1.show();

Student stu2 = new Student();
stu2.show();
}
}

class Student {

int sid;
String sname;

void show() {
System.out.println(sid + "\t" + sname);
}
}

βœ” Output:​

0 null
0 null

πŸ‘‰ Default values:

  • int β†’ 0
  • String β†’ null

Lab330.java​

class Lab330 {

public static void main(String[] args) {
Student stu = new Student();
stu.sid = 99;
stu.sname = "Midhun";
stu.show();
}
}

class Student {

int sid;
String sname;

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

void show() {
System.out.println(sid + "\t" + sname);
}
}

βœ” Output:​

Student Default Constructor
99 Midhun

Lab331.java​

class Lab331 {

public static void main(String[] args) {
Student stu = new Student();
stu.sid = 99;
stu.sname = "Midhun";
stu.show();
}
}

class Student {

int sid;
String sname;

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

void show() {
System.out.println(sid + "\t" + sname);
}
}

βœ” Output:​

Student Default Constructor
99 Midhun

Lab332.java​

class Lab332 {

public static void main(String[] args) {
Student stu = new Student(); // ERROR
stu.sid = 99;
stu.sname = "Midhun";
stu.show();
}
}

class Student {

int sid;
String sname;

Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}

void show() {
System.out.println(sid + "\t" + sname);
}
}

Result:​

  • Compilation Error

πŸ‘‰ Reason:

  • No default constructor exists
  • Only parameterized constructor is defined
  • So new Student() is invalid

🧠 Key Concepts

πŸ”Ή Default Constructor​

  • Provided by JVM only if no constructor is written
  • Initializes variables with default values

πŸ”Ή Parameterized Constructor​

  • Used to initialize variables with custom values
Student(int id,String name){
sid=id;
sname=name;
}

πŸ”Ή Important Rule​

πŸ‘‰ If you write any constructor, JVM will NOT provide default constructor


πŸš€ Quick Summary

ConceptMeaning
ConstructorSpecial method for initialization
No return typeNot even void
Auto executionAt object creation
Default constructorGiven only if none defined
Parameterized constructorCustom initialization

Common Interview Point​

πŸ‘‰ β€œWhy compilation error in Lab332?”

Answer:

  • Because constructor requires arguments
  • But object is created without arguments

Lab333.java​

class Lab333 {

public static void main(String[] args) {
Student stu1 = new Student(88, "Midhun");
stu1.show();

Student stu2 = new Student(99, "Balan");
stu2.show();
}
}

class Student {

int sid;
String sname;

Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}

void show() {
System.out.println(sid + "\t" + sname);
}
}

βœ” Output:​

Student 2-Arg Constructor
88 Midhun
Student 2-Arg Constructor
99 Balan

Lab334.java​

class Lab334 {

public static void main(String[] args) {
Student stu1 = new Student();
stu1.show();

Student stu2 = new Student(99, "Balan");
stu2.show();
}
}

class Student {

int sid;
String sname;

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

Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}

void show() {
System.out.println(sid + "\t" + sname);
}
}

βœ” Output:​

Student Default Constructor
0 null
Student 2-Arg Constructor
99 Balan

Lab335.java​

class Lab335 {

public static void main(String[] args) {
Student stu1 = new Student(99, "Midhun", "sri@JavaWorld", 99999);
stu1.show();

Student stu2 = new Student(88, "Vas", "vas@JavaWorld");
stu2.show();

Student stu3 = new Student(77, "SD");
stu3.show();

Student stu4 = new Student();
stu4.show();
}
}

class Student {

int sid;
String sname;
String email;
long phone;

Student(int id, String sn, String em, long ph) {
System.out.println("Student 4-Arg Constructor");
sid = id;
sname = sn;
email = em;
phone = ph;
}

Student(int id, String sn, String em) {
System.out.println("Student 3-Arg Constructor");
sid = id;
sname = sn;
email = em;
}

Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}

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

void show() {
System.out.println(sid + "\t" + sname + "\t" + email + "\t" + phone);
}
}

βœ” Output:​

Student 4-Arg Constructor
99 Midhun sri@JavaWorld 99999

Student 3-Arg Constructor
88 Vas vas@JavaWorld 0

Student 2-Arg Constructor
77 SD null 0

Student Default Constructor
0 null null 0

🧠 Key Concepts

πŸ”Ή Constructor Overloading​

  • Multiple constructors in the same class with different parameters
  • JVM decides which constructor to call based on arguments

πŸ”Ή Default Values​

TypeDefault
int0
Stringnull
long0

πŸ”Ή Execution Flow​

  • Constructor runs first
  • Then object is ready β†’ methods can be called

πŸ”Ή Important Rules​

  • Constructor name = class name
  • No return type
  • Can overload constructors
  • If no constructor β†’ JVM provides default
  • If any constructor exists β†’ JVM does NOT provide default

πŸš€ Quick Summary

Constructor TypePurpose
DefaultInitialize with default values
ParameterizedInitialize with custom values
OverloadedProvide multiple ways to create object

Interview Insight​

πŸ‘‰ β€œWhich constructor will be called?”

Answer:

  • Depends on number and type of arguments

Lab336.java​

class Lab336 {

public static void main(String[] args) {
Student stu = new Student();
stu.Student(77, "SD"); // trying to call constructor like a method
stu.show();
}
}

class Student {

int sid;
String sname;

Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}

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

void show() {
System.out.println(sid + "\t" + sname);
}
}

Result:​

  • Compilation Error
  • Reason: Constructor cannot be called like a method

Lab337.java​

class Lab337 {

public static void main(String[] args) {
Student stu = new Student();
stu.Student(77, "SD"); // method call
stu.show();
}
}

class Student {

int sid;
String sname;

void Student(int id, String sn) {
// This is NOT a constructor
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}

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

void show() {
System.out.println(sid + "\t" + sname);
}
}

⚠️ Output:​

Student Default Constructor
77 SD

🧠 Explanation:​

  • void Student(...) β†’ method, NOT constructor
  • Because constructor should not have return type
  • Default constructor is executed first
  • Then method is called manually

Lab338.java​

class Lab338 {

public static void main(String[] args) {
Student stu = new Student(77, "SD"); // No matching constructor
stu.show();
}
}

class Student {

int sid;
String sname;

void Student(int id, String sn) {
// This is a method, not constructor
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}

void show() {
System.out.println(sid + "\t" + sname);
}
}

Result:​

  • Compilation Error

🧠 Reason:​

  • No actual constructor Student(int, String) exists
  • Only a method exists β†’ compiler cannot find constructor

πŸš€ Key Concepts from this Page

πŸ”Ή 1. Constructor Rules​

  • Constructor name must match class name
  • No return type (not even void)

πŸ”Ή 2. Common Mistakes​

Calling constructor like a method​

stu.Student(77,"SD");// WRONG

Adding return type​

void Student(...)// This becomes a method, NOT constructor

Missing constructor​

  • If you define no constructor β†’ JVM gives default
  • If you define any constructor β†’ JVM does NOT give default

πŸ”Ή 3. Constructor vs Method​

FeatureConstructorMethod
NameSame as classAny name
Return typeNot allowedRequired
CallAutomaticManual
PurposeInitialize objectPerform operation

Interview Traps from This Page

πŸ‘‰ Q: Why is void Student() not a constructor?

Because constructor must not have return type

πŸ‘‰ Q: Can we call constructor using object?

No β€” only via new

πŸ‘‰ Q: What happens if constructor is missing?

βœ” JVM provides default constructor (only if no other constructor exists)