6.2. Constructors
A constructor is a special method used to initialize objects. It's called when an object is created using the new
keyword.
Constructor Characteristics
- Same name as the class
- No return type (not even void)
- Can have parameters
- Can be overloaded
Types of Constructors
1. Default Constructor
java
public class Car {
private String brand;
private String color;
private int year;
// Default constructor (provided by Java if no constructor is defined)
public Car() {
// Initialize with default values
brand = "Unknown";
color = "White";
year = 2020;
}
}
2. Parameterized Constructor
java
public class Car {
private String brand;
private String color;
private int year;
// Parameterized constructor
public Car(String brand, String color, int year) {
this.brand = brand;
this.color = color;
this.year = year;
}
}
3. Copy Constructor
java
public class Car {
private String brand;
private String color;
private int year;
// Copy constructor
public Car(Car otherCar) {
this.brand = otherCar.brand;
this.color = otherCar.color;
this.year = otherCar.year;
}
}
Constructor Overloading
java
public class Student {
private String name;
private int age;
private String major;
// Constructor 1: Default
public Student() {
this.name = "Unknown";
this.age = 18;
this.major = "Undeclared";
}
// Constructor 2: Name only
public Student(String name) {
this.name = name;
this.age = 18;
this.major = "Undeclared";
}
// Constructor 3: Name and age
public Student(String name, int age) {
this.name = name;
this.age = age;
this.major = "Undeclared";
}
// Constructor 4: All parameters
public Student(String name, int age, String major) {
this.name = name;
this.age = age;
this.major = major;
}
}
this
Keyword in Constructors
java
public class Book {
private String title;
private String author;
private double price;
// Using 'this' to distinguish between instance variables and parameters
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
// Using 'this()' to call another constructor
public Book(String title) {
this(title, "Unknown", 0.0); // Calls the main constructor
}
}
Constructor Chaining
java
public class Employee {
private String name;
private int id;
private String department;
private double salary;
// Base constructor
public Employee(String name, int id) {
this.name = name;
this.id = id;
this.department = "General";
this.salary = 0.0;
}
// Constructor chaining using this()
public Employee(String name, int id, String department) {
this(name, id); // Call base constructor
this.department = department;
}
public Employee(String name, int id, String department, double salary) {
this(name, id, department); // Call previous constructor
this.salary = salary;
}
}
Private Constructors
java
public class UtilityClass {
// Private constructor to prevent instantiation
private UtilityClass() {
throw new AssertionError("Cannot instantiate utility class");
}
public static void utilityMethod() {
System.out.println("This is a utility method");
}
}
Constructor Best Practices
- Initialize all fields in constructors
- Use constructor chaining to avoid code duplication
- Keep constructors simple - avoid complex logic
- Use meaningful parameter names
- Consider using factory methods for complex object creation