Class Constructor
Class Constructor
A school program needs reliable data and clear rules. A constructor initializes a newly created object. Giving a student its name during construction avoids an unnamed intermediate record.
Understand
A constructor initializes a newly created object.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
A constructor has the class name and no return type. A default no-argument constructor is supplied only when no constructor is declared.
Class Constructor
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.
Important boundaries and mistakes
A constructor has the class name and no return type. A default no-argument constructor is supplied only when no constructor is declared.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: A constructor has the class name and no return type.
Java 17 · Main.java · complete practice program
public class Main {
interface Named {
String label();
}
static class Student implements Named {
static String school = "River School";
String name;
Student(String name) {
this.name = name;
}
public String label() {
return name;
}
}
public static void main(String[] args) {
Student s = new Student("Diya");
System.out.println(s.name);
}
}
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
interface Named {
String label();
}
static class Student implements Named {
static String school = "River School";
String name;
Student(String name) {
this.name = name;
}
public String label() {
return name;
}
}
public static void main(String[] args) {
Student s = new Student("Diya");
System.out.println(s.name);
}
}
Expected output
DiyaA constructor initializes a newly created object. Giving a student its name during construction avoids an unnamed intermediate record.
A constructor has the class name and no return type. A default no-argument constructor is supplied only when no constructor is declared.
Run with javac Main.java, then java Main. The declaration inside Main keeps the helper type available to this standalone example.
Check your understanding
Check a common mistake
What to remember
A constructor initializes a newly created object. Giving a student its name during construction avoids an unnamed intermediate record. A constructor has the class name and no return type. A default no-argument constructor is supplied only when no constructor is declared.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.