1) What is Constructor?
A constructor is a special method in Java used to initialize objects. It is automatically called when an object of a class is created.
Name: A constructor must have the same name as the class.
No Return Type: A constructor does not have a return type, not even void.
2) Types of Constructor
Default Constructor:
A default constructor is a constructor with no parameters.
The Java compiler makes a default constructor if we do not write any constructor in our program.
Example:
public class Student{
String name;
int age;
// Default Constructor
public Student(){
System.out.println("Default constructor");
}
public static void main(String[] args){
Student student1 = new Student();
student1.name = "Sasireka";
student1.age = 23;
System.out.println(student1.name);
System.out.println(student1.age);
}
}
Output:
Parameterized Constructor:
A parameterized constructor is used to assign values to object variables when the object is created.
Example:
public class Student {
String name;
int age;
//Parameterized constructor
public Student(String Name, int Age){
name = Name;
age = Age;
}
public static void main(String args[]) {
Student student1 = new Student("Sasireka", 23);
System.out.println(student1.name);
System.out.println(student1.age);
}
}
Output:
3) Constructor Overloading
Constructor overloading - A class have more than one constructor with different no of parameters or with different type of parameter.
Example:
public class Student {
String name;
int age;
//constructor overloading
public Student(String Name, int Age){
name = Name;
age = Age;
}
public Student(){
System.out.println("Constructor");
}
public static void main(String args[]) {
Student student1 = new Student("Sasireka", 23);
System.out.println(student1.name);
System.out.println(student1.age);
Student myStudent1 = new Student();
}
}
Output:
United States
NORTH AMERICA
Related News
How Braze’s CTO is rethinking engineering for the agentic area
11h ago
Amazon Employees Are 'Tokenmaxxing' Due To Pressure To Use AI Tools
22h ago
KDE Receives $1.4 Million Investment From Sovereign Tech Fund
2h ago
Instagram’s new ‘Instants’ feature combines elements from Snapchat and BeReal
2h ago
Six Claude Code Skills That Close the AI Agent Feedback Loop
2h ago



