Constructors in Java
A constructor in Java is a special type of method used to initialize objects. It is called when an object of a class is created. Constructors have the same name as the class and do not have a return type (not even void).
Key Characteristics of Constructors
- Same Name as Class: The constructor’s name must match the class name.
- No Return Type: Constructors do not return any value, not even void.
- Automatic Invocation: They are automatically called when an object is created.
- Purpose: To initialize an object, typically setting initial values for fields or performing setup operations.
Types of Constructors
1. Default Constructor
- A constructor provided by Java if no constructor is explicitly defined in a class.
- It has no parameters and initializes object fields with default values (e.g., 0 for integers, null for objects, etc.).
Example: Default Constructor
class Test {
int value; // Default value is 0
void display() {
System.out.println(“Value: ” + value);
}
}
public class Main {
public static void main(String[] args) {
Test obj = new Test(); // Default constructor is called
obj.display();
}
}
Output:
Value: 0
2. No-Argument Constructor
- A user-defined constructor with no parameters.
Example: No-Argument Constructor
class Test {
Test() {
System.out.println(“No-Argument Constructor Called”);
}
}
public class Main {
public static void main(String[] args) {
Test obj = new Test();
}
}
Output:
No-Argument Constructor Called
3. Parameterized Constructor
- A constructor that takes arguments to initialize object fields with specific values.
Example: Parameterized Constructor
class Car {
String brand;
int speed;
// Parameterized Constructor
Car(String b, int s) {
brand = b;
speed = s;
}
void display() {
System.out.println(“Brand: ” + brand + “, Speed: ” + speed);
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car(“Tesla”, 200);
Car car2 = new Car(“BMW”, 250);
car1.display();
car2.display();
}
}
Output:
Brand: Tesla, Speed: 200
Brand: BMW, Speed: 250
Key Points About Constructors
1. Constructor Overloading
- You can define multiple constructors with different parameter lists in a single class (like method overloading).
Example: Constructor Overloading
class Person {
String name;
int age;
// No-argument constructor
Person() {
name = “Unknown”;
age = 0;
}
// Parameterized constructor
Person(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(“Name: ” + name + “, Age: ” + age);
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person(“Alice”, 25);
p1.display();
p2.display();
}
}
Output:
Name: Unknown, Age: 0
Name: Alice, Age: 25
2. Constructor with this Keyword
- The this keyword can be used to refer to the current object or to call another constructor in the same class.
Example: Using this for Constructor Chaining
class Person {
String name;
int age;
// Constructor 1
Person() {
this(“Unknown”, 0); // Calls Constructor 2
}
// Constructor 2
Person(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(“Name: ” + name + “, Age: ” + age);
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person(“Alice”, 30);
p1.display();
p2.display();
}
}
Output:
Name: Unknown, Age: 0
Name: Alice, Age: 30
3. Constructor vs Method
Aspect | Constructor | Method |
Name | Same as the class name | Any valid name |
Return Type | No return type | Must have a return type |
Called | Automatically when an object is created | Explicitly by the programmer |
Purpose | To initialize an object | To define behavior or actions |
Common Mistakes
- Returning a Value from a Constructor
Constructors cannot return a value. - Defining a Constructor Without Calling It
If no constructor is explicitly called, Java uses the default constructor (if defined or available).
Conclusion Constructors in Java play a vital role in object initialization. Whether you use default, no-argument, or parameterized constructors, they enable efficient and flexible initialization of objects. With constructor overloading and chaining, Java allows you to create objects with varying initialization needs.