VOOZH about

URL: https://www.geeksforgeeks.org/java/constructors-in-java/

⇱ Java Constructors - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Constructors

Last Updated : 26 Mar, 2026

A constructor in Java is a special member that is called when an object is created. It initializes the new object’s state. It is used to set default or user-defined values for the object's attributes

  • A constructor has the same name as the class.
  • It does not have a return type, not even void.
  • It can accept parameters to initialize object properties.

Output
Name: Vishnu

Types of Constructors in Java

There are four types of constructors in Java

👁 constructors_in_java
Constructor

1. Default Constructor

A default constructor has no parameters. It’s used to assign default values to an object. If no constructor is explicitly defined, Java provides a default constructor.


Output
Default constructor

Note: It is not necessary to write a constructor for a class because the Java compiler automatically creates a default constructor (a constructor with no arguments) if your class doesn’t have any.

2. Parameterized Constructor

A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor.


Output
GeekName: Sweta and GeekId: 68

3. Copy Constructor in Java

Unlike other constructors copy constructor is passed with another object which copies the data available from the passed object to the newly created object.


Output
First Object
GeekName: Sweta and GeekId: 68

Copy Constructor used Second Object
GeekName: Sweta and GeekId: 68

Explanation: This depicts a copy constructor, where the second Geeks object (geek2) is created by passing the first object (geek1) to the constructor. The copy constructor copies name and id from geek1 to geek2, resulting in two objects with the same data.

Note: Java does not provide a built-in copy constructor like C++. We can create our own by writing a constructor that takes an object of the same class as a parameter and copies its fields.

4. Private Constructor

A private constructor cannot be accessed from outside the class. It is commonly used in:

  • Singleton Pattern: To ensure only one instance of a class is created.
  • Utility/Helper Classes: To prevent instantiation of a class containing only static methods.

Output
Hello from GFG class!

Explanation: The GFG constructor is declared private, so an object of GFG cannot be created in main(). The class is accessed only through the static method displayMessage(), which is called directly using the class name.

Constructor Overloading

This is a key concept in OOP related to constructors is constructor overloading. This allows us to create multiple constructors in the same class with different parameter lists.


Output
Constructor with one argument - String: Sweta
Constructor with two arguments: String and Integer: Amiya 28
Constructor with one argument: Long: 325614567

Explanation: Demonstrates constructor overloading by defining multiple Geeks constructors with different parameter lists. Based on the arguments passed while creating objects (geek2, geek3, geek4), the corresponding constructor is invoked at compile time.

Related Articles:

Comment
Article Tags: