⬅ Previous Topic
Java ClassNext Topic ⮕
Java Class Objects⬅ Previous Topic
Java ClassNext Topic ⮕
Java Class ObjectsIn Java, a constructor is a special method that's invoked when an object of a class is created. Unlike regular methods, constructors have the same name as the class and do not have a return type.
The main purpose of a constructor is to initialize objects.
class ClassName {
ClassName() {
// constructor body
}
}
Java provides three types of constructors:
If you don't explicitly define any constructor, Java provides one by default. It initializes the object with default values.
class Student {
Student() {
System.out.println("Default Constructor called.");
}
void display() {
System.out.println("Student details loaded.");
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Default Constructor called.
Student details loaded.
When you want to initialize an object with custom values, parameterized constructors come into play.
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Alice", 20);
Student s2 = new Student("Bob", 22);
s1.display();
s2.display();
}
}
Name: Alice
Age: 20
Name: Bob
Age: 22
Java doesn’t provide a built-in copy constructor like C++, but you can create one manually to copy values from another object.
class Student {
String name;
int age;
// Parameterized constructor
Student(String n, int a) {
name = n;
age = a;
}
// Copy constructor
Student(Student s) {
name = s.name;
age = s.age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Clara", 23);
Student s2 = new Student(s1); // copying s1 into s2
s2.display();
}
}
Name: Clara, Age: 23
You can define multiple constructors in a class with different parameter lists. This is called constructor overloading.
class Rectangle {
int length, breadth;
// Constructor 1
Rectangle() {
length = 0;
breadth = 0;
}
// Constructor 2
Rectangle(int l, int b) {
length = l;
breadth = b;
}
void display() {
System.out.println("Length: " + length + ", Breadth: " + breadth);
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(5, 3);
r1.display();
r2.display();
}
}
Length: 0, Breadth: 0
Length: 5, Breadth: 3
void
.Constructors lay the foundation for your objects. They’re not just initializers—they help set the tone for object identity, behavior, and consistency right at the start. Think of them as the blueprint’s first breath into a living, breathing object in your program.
class Demo {
Demo() {
System.out.println("Default constructor");
}
public static void main(String[] args) {
new Demo();
}
}
Student s1 = new Student("Alice", 20);
⬅ Previous Topic
Java ClassNext Topic ⮕
Java Class ObjectsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.