Constructors in Java
A constructor in Java is a special block of code used to initialize objects. It has the same name as the class and is called automatically when an object of the class is created. Constructors are essential for setting up the initial state of an object.
Characteristics of Constructors
- Same Name as Class: A constructor must have the same name as the class.
- No Return Type: Constructors do not specify a return type, not even
void
. - Automatic Invocation: Called when an object is created using the
new
keyword. - Purpose: Primarily used to initialize variables of a class or perform setup tasks.
Types of Constructors
- Default Constructor (No-Argument Constructor)
- Parameterized Constructor
1. Default Constructor
A default constructor is a constructor that does not take any parameters. If no constructor is defined in a class, Java provides a default constructor automatically. However, if you define a constructor, the default constructor is no longer provided automatically.
Characteristics:
- Has no parameters.
- Initializes fields with default values:
0
for numeric types.null
for objects.false
forboolean
.- Useful for creating objects without any specific initial values.
Example:
class MyClass {
int num; // Default value will be 0
String text; // Default value will be null
// Default constructor
MyClass() {
System.out.println("Default constructor called");
}
}
public class Test {
public static void main(String[] args) {
MyClass obj = new MyClass(); // Default constructor is called
System.out.println("num: " + obj.num); // Output: num: 0
System.out.println("text: " + obj.text); // Output: text: null
}
}
2. Parameterized Constructor
A parameterized constructor takes arguments to initialize an object with specific values. It provides more flexibility by allowing different initial values for different objects.
Characteristics:
- Takes parameters to set the values of fields.
- Enables object initialization with user-defined values.
Example:
class MyClass {
int num;
String text;
// Parameterized constructor
MyClass(int number, String str) {
num = number;
text = str;
System.out.println("Parameterized constructor called");
}
}
public class Test {
public static void main(String[] args) {
MyClass obj1 = new MyClass(42, "Hello");
MyClass obj2 = new MyClass(100, "World");
System.out.println("obj1 - num: " + obj1.num + ", text: " + obj1.text);
// Output: obj1 - num: 42, text: Hello
System.out.println("obj2 - num: " + obj2.num + ", text: " + obj2.text);
// Output: obj2 - num: 100, text: World
}
}
Key Differences Between Default and Parameterized Constructor
Aspect | Default Constructor | Parameterized Constructor |
---|---|---|
Parameters | No parameters. | Takes one or more parameters. |
Initialization | Initializes fields with default values. | Initializes fields with user-defined values. |
Purpose | Creates generic objects. | Creates specific objects with custom values. |
Provided by Java | Automatically provided if no constructor exists. | Must be explicitly defined by the programmer. |
Conclusion
Use the default constructor when you need to create objects with default or generic values. Use the parameterized constructor to initialize objects with specific values during their creation.