Constructors in java are special methods that are called when an object is created. They are used to initialize the state of an object and perform any necessary setup tasks.
Table of Contents
Types of Constructors
There are five types of Constructors in Java that are listed below:
- Default Constructor.
- Parameterized Constructor.
- Copy Constructor
- Private Constructor.
- Static Constructor.
Default Constructor
A default constructor is a constructor that is automatically generated by the compiler when no constructor is defined in the class. It takes no arguments and initializes the object with default values. In Java, the default values for integer types are 0, for boolean it’s false and for object references, it’s null.
public class MyClass {
public MyClass() {
// Code for default constructor
}
}
Parameterized Constructor
A parameterized constructor is a constructor that takes one or more arguments and initializes the object with those values. These arguments are passed when the object is created. This is useful when you want to initialize an object with specific values.
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Copy Constructor
A copy constructor is used to create a new object that is a copy of an existing object. This is useful when you want to create a new object with the same state as an existing object.
public class Person {
private String name;
private int age;
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
}
Private Constructor
A private constructor is used to restrict the creation of objects from outside the class. This is useful when you want to control how objects of a class are created. A common use case for a private constructor is when implementing the Singleton design pattern.
public class Singleton {
private static Singleton instance;
private Singleton() {
// Code for private constructor
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Static Constructor
A static constructor is used to initialize static variables or perform other static operations. Static constructors are only called once, when the class is loaded.
public class MyClass {
static {
// Code for static constructor
}
}
Code Example
public class ExampleClass {
private int num1;
private double num2;
private String str;
// default constructor
public ExampleClass() {
num1 = 0;
num2 = 0.0;
str = "";
}
// parameterized constructor
public ExampleClass(int num1, double num2, String str) {
this.num1 = num1;
this.num2 = num2;
this.str = str;
}
// copy constructor
public ExampleClass(ExampleClass other) {
this.num1 = other.num1;
this.num2 = other.num2;
this.str = other.str;
}
// private constructor
private ExampleClass(double num2) {
this.num2 = num2;
this.num1 = 0;
this.str = "";
}
// static factory method
public static ExampleClass createInstance(int num1, double num2, String str) {
ExampleClass obj = new ExampleClass(num1, num2, str);
obj.num1 += 100;
obj.num2 += 10.0;
obj.str += " (created using factory method)";
return obj;
}
// getter methods for private fields
public int getNum1() {
return num1;
}
public double getNum2() {
return num2;
}
public String getStr() {
return str;
}
// main method for testing
public static void main(String[] args) {
// create objects using different constructors
ExampleClass obj1 = new ExampleClass();
ExampleClass obj2 = new ExampleClass(1, 2.5, "hello");
ExampleClass obj3 = new ExampleClass(obj2);
ExampleClass obj4 = new ExampleClass(2.5);
ExampleClass obj5 = ExampleClass.createInstance(3, 4.5, "world");
// print the fields of each object
System.out.println("Object 1: num1 = " + obj1.getNum1() + ", num2 = " + obj1.getNum2() + ", str = " + obj1.getStr());
System.out.println("Object 2: num1 = " + obj2.getNum1() + ", num2 = " + obj2.getNum2() + ", str = " + obj2.getStr());
System.out.println("Object 3: num1 = " + obj3.getNum1() + ", num2 = " + obj3.getNum2() + ", str = " + obj3.getStr());
System.out.println("Object 4: num1 = " + obj4.getNum1() + ", num2 = " + obj4.getNum2() + ", str = " + obj4.getStr());
System.out.println("Object 5: num1 = " + obj5.getNum1() + ", num2 = " + obj5.getNum2() + ", str = " + obj5.getStr());
}
}
Output

Conclusion
Constructors are an essential part of object-oriented programming in Java. By understanding the different types of constructors and their usage, you can create more efficient and effective programs. By implementing the code examples provided, you can see the different types of constructors in action and get a better understanding of how they can be used in your own programs.