Java Constructor

Constructor in Java is a special block of code used to create the instance of class which is invoked automatically when an object of the class is created. It has the same name as the class name and has no return type.

Syntax :

class ClsName

{   
    // A Constructor
    ClsName() {

    }
}

ClsName obj = new ClsName(); 

The first line of a constructor is a call to super() or this(), which is a call to a constructor of a super-class or an overloaded constructor.Even if you call super() in your constructor the compiler will automatically add super() at the first line of your code. The super constructor must be called to create an object.

Program : Java Program for Contructor usage

public class KsamyatamConstruct {

	KsamyatamConstruct()
    {
        super();
        System.out.println("Constructor Call,Welcome To Ksamyatam World!");
    }

    public static void main(String[] args)
    {
    	KsamyatamConstruct ksamyatam = new KsamyatamConstruct();
    }
}
Output :

Constructor Call,Welcome To Ksamyatam World!

Constructors Features :

Same Name as the Class: A constructor has the same name as the class.

No Return Type: Constructors do not have any return type, not even void as constructor is used to initialize the object, not to return a value.

Called Automatically On Object Creation: When an object of a class is created, the constructor is called automatically even when it is not defined.

Used to Set Initial Values for Object Attributes: Constructors primarily purpose is to set the initial state or values of an object’s attributes as soon as it is created.

Constructor Type :

There are three types of constructor in java.

  • Default Constructor
  • No-Args Constructor
  • Parameterized Constructor
  • Copy Constructor

Default Constructor in Java :

If we do not create any constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program which is called Default Constructor.

Program : Java Program for Default Constructor :

public class DefConstructor {

	  int num;
	  boolean bool_val;

	  public static void main(String[] args) {

	    DefConstructor obj = new DefConstructor();

	    System.out.println("Default Value:");
	    System.out.println("Number = " + obj.num);
	    System.out.println("Boolean = " + obj.bool_val);
	  }
}
Output :

Default Value:
Number = 0
Boolean = false

Java No-Arg Constructors

A Java constructor may or may not have any parameters (arguments) just like methods. When a constructor does not accept any parameters, it is known as a no-argument constructor.

Syntax for Java No-Arg Constructors :

private Constructor() {
   // body of the constructor
}

Progam : Java Program for No-Arg Constructors

public class NoArgConstruct {
	  
	  int num;

	  private NoArgConstruct() {
	    num = 10;
	    System.out.println("Constructor Call");
	  }

	  public static void main(String[] args) {

	    NoArgConstruct obj = new NoArgConstruct();
	    System.out.println("Value of num : " + obj.num);
	  }
}
Output :

Constructor Call
Value of num : 10

Program : Java Program For Public no-arg Constructors

public class Department {

	   String subject;

	  // public constructor
	  public Department() {
	    subject = "Java Programming";
	  }
}
public class PublicNoArgsConstruct {
	public static void main(String[] args) {

	    Department obj = new Department();
	    System.out.println("Subject Name : " + obj.subject);
	  }
}
Output :
Subject Name : Java Programming

Java Parameterized Constructor :

A Java parameterized constructor is a constructor which can have one or more parameters.

Program : Java Program for Parameterized Constructor

public class KsamyatamPC {
	
	  String programming;

	  KsamyatamPC(String prog) {
	    programming = prog;
	    System.out.println(programming + " Programming Language ");
	  }

	  public static void main(String[] args) {

		  KsamyatamPC obj1 = new KsamyatamPC("C");
		  KsamyatamPC obj2 = new KsamyatamPC("C++");
		  KsamyatamPC obj3 = new KsamyatamPC("Java");
		  KsamyatamPC obj4 = new KsamyatamPC("Python");

	  }
}
Output :

C Programming Language 
C++ Programming Language 
Java Programming Language 
Python Programming Language 

Java Copy Constructor :

While Java does not have a built-in concept of a “copy constructor” like C++, you can implement a similar functionality. We can create our own by writing a constructor that takes an object of the same class as a parameter and copies its fields.

Program : Java Program for Copy Constructor

public class CopyConstruct {

	String stud_name;
    int stud_id;

    CopyConstruct(String stud_name, int stud_id)
    {
        this.stud_name = stud_name;
        this.stud_id = stud_id;
    }

    CopyConstruct(CopyConstruct obj2)
    {
        this.stud_name = obj2.stud_name;
        this.stud_id = obj2.stud_id;
    }
}
public class KsamyatamCopyConstruct {
	public static void main(String[] args)
    {
        System.out.println("Initial Object");
        CopyConstruct obj1 = new CopyConstruct("Sachin", 55);
        System.out.println("Student Name : " + obj1.stud_name
                           + " and ID : " + obj1.stud_id);

        System.out.println();

        CopyConstruct obj2 = new CopyConstruct(obj1);
        System.out.println(
            "Second Object From Copy Constructor");
        System.out.println("Student Name : " + obj2.stud_name
                           + " and ID : " + obj2.stud_id);
    }
}
Output :

Initial Object
Student Name : Sachin and ID : 55

Second Object From Copy Constructor
Student Name : Sachin and ID : 55

Constructors Overloading in Java

Just like Java method overloading, we can also create two or more constructors with different parameters which is called constructor overloading.

Program : Java Program for Constructor Overloading

public class ProgDepartment {

	  String language;

	  ProgDepartment() {
	    this.language = "Python";
	  }

	  ProgDepartment(String language) {
	    this.language = language;
	  }

	  public void getName() {
	    System.out.println("Programming Language Applied For : " + this.language);
	  }

	  public static void main(String[] args) {

		  ProgDepartment obj1 = new ProgDepartment();

		  ProgDepartment obj2 = new ProgDepartment("Java");

	      obj1.getName();
	      obj2.getName();
	  }
}
Output :

Programming Language Applied For : Python
Programming Language Applied For : Java
Scroll to Top