Java Constructor
A constructor in Java is a special method used to initialize objects. Unlike regular methods, a constructor is automatically called when a new instance of a class is created. Constructors help set up initial values for object attributes and ensure your objects start life in a valid state.
Example:
Output:
Example:
Output:
Output:
Output:
Types of Constructors in Java
1. Default Constructor
A default constructor is a constructor with no parameters. If you don’t define any constructor in your class, Java automatically provides a default constructor that initializes object fields to default values (like 0 for integers, null for objects).
Syntax:
1. Default Constructor
A default constructor is a constructor with no parameters. If you don’t define any constructor in your class, Java automatically provides a default constructor that initializes object fields to default values (like 0 for integers, null for objects).
Syntax:
ClassName() { // constructor body}Example:
class Message { Message() { System.out.println("QUIPO HOUSE"); }
public static void main(String[] args) { Message obj = new Message(); // Calls default constructor }}Output:
QUIPO HOUSE2. Parameterized Constructor
A parameterized constructor accepts arguments, allowing you to assign specific values to object fields at the time of creation.
Syntax:
A parameterized constructor accepts arguments, allowing you to assign specific values to object fields at the time of creation.
Syntax:
ClassName(type param1, type param2) { // constructor body}Example:
public class Identifier { String msg, msg1;
Identifier(String msg, String msg1) { this.msg = msg; this.msg1 = msg1; }
void message() { System.out.println(msg + " " + msg1); }
public static void main(String[] args) { Identifier obj = new Identifier("Message", "msg"); obj.message(); }}Output:
Message msg3. Copy Constructor
Java does not provide a built-in copy constructor, but you can create one to copy the values from one object to another.
Example:
Java does not provide a built-in copy constructor, but you can create one to copy the values from one object to another.
Example:
class Employee { String name; int age;
// Parameterized Constructor Employee(String name, int age) { this.name = name; this.age = age; }
// Copy Constructor Employee(Employee e) { this.name = e.name; this.age = e.age; }
void display() { System.out.println("Name: " + name + ", Age: " + age); }
public static void main(String[] args) { Employee e1 = new Employee("John", 25); Employee e2 = new Employee(e1); // Using copy constructor
e1.display(); e2.display(); }}Output:
Name: John, Age: 25
Name: John, Age: 254. Constructor Chaining
Constructor chaining allows one constructor to call another constructor in the same class using this(). This is useful for code reusability and reducing duplication.
Example:
Constructor chaining allows one constructor to call another constructor in the same class using this(). This is useful for code reusability and reducing duplication.
Example:
class Student { String name; int age;
// Default Constructor calling Parameterized Constructor Student() { this("Unknown", 0); }
Student(String name, int age) { this.name = name; this.age = age; }
void display() { System.out.println("Name: " + name + ", Age: " + age); }
public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Alice", 22); s1.display(); s2.display(); }}Output:
Name: Unknown, Age: 0
Name: Alice, Age: 22Constructor vs Method: Key Differences
| Feature | Constructor | Method |
|---|---|---|
| Name | Same as class name | Any valid identifier |
| Return Type | No return type ( not even void ) | Must have a return type |
| Call | Called automatically on object creation | Must be called explicitly |
| Overloading | Yes | Yes |
| Overriding | No | Yes |
Key Points
- A constructor must have the same name as the class.
- Constructors do not have a return type.
- Constructors cannot be final, static, abstract, or synchronized.
- If no constructor is defined, Java provides a default one automatically.
- You can overload constructors by defining multiple constructors with different parameter lists.
Tips: Use constructors to ensure your objects are always created with valid, meaningful data. Constructor chaining and copy constructors can help you write cleaner, more reusable code.
Need more clarification?
Drop us an email at career@quipoinfotech.com
