Use of Constructor in Java

Use of Constructor in Java

5 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Sep 12, 2024 16:21 IST

Have you ever wondered how Java objects are initialized? Using constructors in Java is crucial for creating and setting up objects. Constructors allow developers to initialize object states and ensure proper setup with default or specified values, enhancing the flexibility of object-oriented programming. Let's understand more!

Constructors in Java are special methods used to initialize objects. They are called when an instance of a class is created and has the same name as the class. Unlike regular methods, constructors do not have a return type, not even void.

Check out Java Courses Here!

Types of Constructors in Java

Some Uses of Constructor in Java

  •  Initializing Objects
  • Constructor Overloading
  • Default Constructor
  • Constructor Chaining
  • Copy Constructor

Let's see one by one in detail!

1. Initializing Objects

Constructors are primarily used to set initial values for the instance variables of the class.

For Example,


 
public class Example {
private int value;
// Constructor to initialize value
public Example(int value) {
this.value = value;
}
public void display() {
System.out.println("Value: " + this.value);
}
public static void main(String[] args) {
Example vishal = new Example(10);
vishal.display();
}
}
Copy code

Output

Value: 10

This constructor initializes the object with a specific value, ensuring the object starts with a defined state.

2. Constructor Overloading

Java allows multiple constructors in a class, each with different parameters. This is known as constructor overloading, enabling the creation of objects in multiple ways.

For Example,


 
public class Example {
private int value;
private String name;
// Constructor with one parameter
public Example(int value) {
this.value = value;
this.name = "Unknown";
}
// Constructor with two parameters
public Example(int value, String name) {
this.value = value;
this.name = name;
}
public void display() {
System.out.println("Value: " + this.value + ", Name: " + this.name);
}
public static void main(String[] args) {
Example vishal1 = new Example(10);
Example vishal2 = new Example(20, "Vishal");
vishal1.display();
vishal2.display();
}
}
Copy code

Output

Value: 10, Name: Unknown
Value: 20, Name: Vishal

Constructor overloading allows flexibility in object creation by supporting multiple initialization options.

3. Default Constructor

If no constructor is explicitly defined, Java provides a default constructor that initializes the object with default values.

For Example,


 
public class Example {
private int value;
private String name;
// Default constructor
public Example() {
this.value = 0;
this.name = "Unknown";
}
public void display() {
System.out.println("Value: " + this.value + ", Name: " + this.name);
}
public static void main(String[] args) {
Example vishal = new Example();
vishal.display();
}
}
Copy code

Output

Value: 0, Name: Unknown

The default constructor provides a way to create an object with default values when no parameters are provided.

4. Constructor Chaining

Constructors can call other constructors in the same class using this keyword. This helps reduce code duplication.

For Example,


 
public class Example {
private int value;
private String name;
// Default constructor
public Example() {
this(0, "Unknown");
}
// Constructor with one parameter
public Example(int value) {
this(value, "Unknown");
}
// Constructor with two parameters
public Example(int value, String name) {
this.value = value;
this.name = name;
}
public void display() {
System.out.println("Value: " + this.value + ", Name: " + this.name);
}
public static void main(String[] args) {
Example vishal = new Example();
vishal.display();
}
}
Copy code

Output

Value: 0, Name: Unknown

Constructor chaining reduces code duplication by reusing other constructors to set default values.

5.  Copy Constructor

A constructor that creates a new object as a copy of an existing object.

Copy Constructor In Java

For Example,


 
public class Example {
private int value;
private String name;
// Constructor with two parameters
public Example(int value, String name) {
this.value = value;
this.name = name;
}
// Copy constructor
public Example(Example other) {
this.value = other.value;
this.name = other.name;
}
public void display() {
System.out.println("Value: " + this.value + ", Name: " + this.name);
}
public static void main(String[] args) {
Example vishal1 = new Example(20, "Vishal");
Example vishal2 = new Example(vishal1);
vishal1.display();
vishal2.display();
}
}
Copy code

Output

Value: 20, Name: Vishal
Value: 20, Name: Vishal

The copy constructor creates a new object with the same state as an existing object, useful for duplicating objects.

Constructors in Java Explained

Understanding Advantages of Java

Understanding Java Main Method

Benefits of OOPs in Java

Types of Inheritance in Java
 

Thus, the examples above show the various uses and functionalities of constructors in Java, highlighting their importance in object initialization and class design.

FAQs

What is a constructor in Java?

A constructor in Java is a special method that initializes objects. It is called when an instance of a class is created and has the same name as the class. Unlike regular methods, constructors do not have a return type.

How does a constructor differ from a regular method?

A constructor initializes an object and has no return type, not even void. It must have the same name as the class. Regular methods perform operations and can have any name and return type.

What is constructor overloading?

Constructor overloading is the practice of defining multiple constructors in the same class, each with different parameters. This allows objects to be initialized in various ways.

What is a default constructor?

A default constructor is a constructor provided by Java if no constructors are explicitly defined. It initializes the object with default values.

What is the purpose of the this keyword in constructors?

The this keyword is used within a constructor to refer to the current instance of the class. It is commonly used to differentiate between instance variables and parameters with the same name and to call other constructors within the same class (constructor chaining).

What is constructor chaining and why is it useful?

Constructor chaining is the process of calling one constructor from another constructor within the same class using the this keyword. It helps in reducing code duplication and allows for better organization of initialization logic.

Can constructors be private and why would you use a private constructor?

Yes, constructors can be private. Private constructors are used in singleton patterns to restrict instantiation of a class to one object, and in factories or utility classes to prevent instantiation.

What is a copy constructor?

A copy constructor is a constructor that initializes an object using another object of the same class. It creates a copy of the given object.

Can a constructor call a method within the same class?

Yes, a constructor can call methods within the same class. This is useful for initializing objects using methods that perform complex setup procedures.

What happens if a class does not have any constructors defined?

If a class does not have any constructors defined, Java provides a default constructor that initializes the object with default values (zero, null, false, etc.).

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio