Constructors in Java are special methods within a class that are used to initialize objects of that class. They have the same name as the class and do not have a return type, not even void. Constructors are automatically invoked when an object is created using the new keyword.
Key Points about Constructors
Name and Signature: Constructors have the same name as the class. They can be parameterized or non-parameterized, depending on whether they accept arguments.
public class MyClass {
// Non-parameterized constructor
public MyClass() {
// Constructor logic
}
// Parameterized constructor
public MyClass(int parameter1, String parameter2) {
// Constructor logic with parameters
}
}
Code language: Java (java)
Initialization: Constructors are responsible for initializing the instance variables of an object. This ensures that the object is in a valid and usable state upon creation.
Implicit and Explicit Invocation: Constructors are implicitly invoked when an object is created.
MyClass obj = new MyClass(); // Implicitly invokes the non-parameterized constructor
Code language: JavaScript (javascript)
Constructors can also be explicitly invoked if there are multiple constructors in a class, and the developer wants to choose a specific one.
MyClass obj = new MyClass(42, "Hello"); // Explicitly invokes the parameterized constructor
Code language: JavaScript (javascript)
Chaining Constructors Overloading: Constructors can call other constructors in the same class using this(). This allows for constructor overloading, providing flexibility in object initialization.
public class MyClass {
public MyClass() {
// Non-parameterized constructor logic
}
public MyClass(int parameter) {
this(); // Calls the non-parameterized constructor
// Parameterized constructor logic
}
}
Code language: PHP (php)
Constructor Types
In Java, there are two main types of constructors: Default Constructors and Parameterized Constructors. Let’s explore each type:
Default Constructor
A default constructor is a constructor that is automatically provided by Java if no constructor is explicitly defined in a class. It does not take any parameters. The default constructor initializes the instance variables with default values (e.g., 0 for numeric types, null for objects, etc.).
Syntax:
public class MyClass {
// Default constructor
public MyClass() {
// Constructor logic (default initialization)
}
}
Code language: PHP (php)
Here’s an example that demonstrates default constructor:
class Book { // Fields private String title; private String author; // Default constructor public Book() { // Default values this.title = "Java Programming"; this.author = "E.BalaGuru Swamy"; } // Method to display information about the book public void displayInfo() { System.out.println("Title: " + title); System.out.println("Author: " + author); } } class DefaultDemo{ public static void main(String[] args) { // Creating objects using the constructors Book defaultBook = new Book(); // Default constructor // Displaying information using object methods System.out.println("Default Book:"); defaultBook.displayInfo(); } }
Output:
D:>java DefaultConstructorDemo
Default Book:
Title: Java Programming
Author: E.BalaGuru Swamy
Code language: CSS (css)
Parameterized Constructor
A parameterized constructor is a constructor that accepts parameters during object creation. It allows you to initialize the instance variables with values provided at the time of object instantiation.
Syntax:
public class MyClass {
// Parameterized constructor
public MyClass(DataType parameter1, DataType parameter2, ...) {
// Constructor logic (initialization using parameters)
}
}
Code language: PHP (php)
Copy Constructor
A copy constructor is a type of constructor that takes an object of the same class as a parameter and creates a new object with the same values. It is not provided by default, and you need to define it explicitly.
Syntax
public class MyClass {
// Fields
private DataType field1;
private DataType field2;
// Copy constructor
public MyClass(MyClass otherObject) {
this.field1 = otherObject.field1;
this.field2 = otherObject.field2;
}
// Other methods and constructors...
}
Code language: PHP (php)
Here’s an example that demonstrates parameterized and copy constructors:
class Person { // Fields private String name; private int age; // Parameterized constructor public Person(String name, int age) { this.name = name; this.age = age; } // Copy constructor public Person(Person otherPerson) { this.name = otherPerson.name; this.age = otherPerson.age; } // Method to display information about the person public void displayInfo() { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); } } class ParamCopy{ public static void main(String[] args) { // Creating an object using the parameterized constructor Person originalPerson = new Person("Mahesh", 21); // Creating a new object using the copy constructor Person copiedPerson = new Person(originalPerson); // Displaying information using object methods System.out.println("Original Person:"); originalPerson.displayInfo(); System.out.println("\nCopied Person:"); copiedPerson.displayInfo(); } }
Output:
D:>java ParameterCopyDemo
Original Person:
Name: Mahesh
Age: 21 years
Copied Person:
Name: Mahesh
Age: 21 years