- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java
Java Class Constructor
Types, Syntax & Examples
In 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.
Basic Syntax of a Constructor
class ClassName {
ClassName() {
// constructor body
}
}
Types of Constructors in Java
Java provides three types of constructors:
- Default Constructor
- Parameterized Constructor
- Copy Constructor (custom implemented)
1. Default Constructor
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.
2. Parameterized Constructor
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
3. Copy Constructor
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
Constructor Overloading
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
Key Points to Remember
- Constructors don’t have a return type, not even
void
. - They are automatically invoked when an object is created.
- You can overload constructors with different parameter sets.
- If no constructor is defined, Java provides a default one.
- Copy constructors are not predefined, but you can define them manually.
Why Constructors Matter?
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.
QUIZ
Question 1:Which of the following statements about Java constructors is true?
Question 2:If no constructor is explicitly defined in a Java class, the compiler automatically inserts a default constructor.
Question 3:What will be the output of the following code?
class Demo {
Demo() {
System.out.println("Default constructor");
}
public static void main(String[] args) {
new Demo();
}
}
class Demo {
Demo() {
System.out.println("Default constructor");
}
public static void main(String[] args) {
new Demo();
}
}
Question 4:Which of the following are valid characteristics or behaviors of Java constructors?
Question 5:Copy constructors in Java are provided by default, similar to default constructors.
Question 6:In the code below, which constructor is being used for object creation?
Student s1 = new Student("Alice", 20);
Student s1 = new Student("Alice", 20);