Constructors in Java

Constructors in Java

In this tutorial, we are going to discuss constructors in java. Constructors in Java are special types of methods that are used to initialize the objects of the class. Constructors are called at the time of object creation of the class.

Constructors in Java

Just like methods, although they hold a set of lines of code, they are quite different from them. Constructors have the same name as the Java class, but it does not have any return type.

  • After the Creation of an Object is compulsory, we should perform initialization then only that object is in a position to provide service to others.
  • At the time of Object Creation, some piece of code will execute automatically to perform initialization. That piece of code is nothing but a “Constructor.”
  • Hence the main objective of the constructor is to perform initialization.
  • If we don’t have a constructor, we will face a burden. For example, see the following code.
public class Student {
   String name;
   int rollno;
   public static void main(String[] args) {
      Student s1 = new Student();
      Student s2 = new Student();
      Student s3 = new Student();
      Student s4 = new Student();
      s1.name = "Ashok";
      s1.rollno = 101;
      
      s2.name = "Vinod";
      s2.rollno = 102;
      
      s3.name = "Dillesh";
      s3.rollno = 103;
      
      s4.name = "Thiru";
      s4.rollno = 104;
      System.out.println(s1.name+"-----"+s1.rollno);
      System.out.println(s2.name+"-----"+s2.rollno);
      System.out.println(s3.name+"-----"+s3.rollno);
      System.out.println(s4.name+"-----"+s4.rollno);
    }
}

To over come this type of burden constructor was introduced.

public class Student {
   String name;
   int rollno;
   Student(String name, int rollno) {
      this.name = name;
      this.rollno = rollno;
   }
   public static void main(String[] args) {
      Student s1 = new Student("raju",101);
      Student s2 = new Student("mani",102);
      System.out.println(s1.name+"-----"+s1.rollno);
      System.out.println(s2.name+"-----"+s2.rollno);
   }
}
Rules for Constructor

While writing constructors we should follow the following rules.

  • The name of the constructor and the name of the class must be the same.
  • The only allowed modifiers for the constructors are public, private, protected, . If we are using any other modifier, we will get C.E(Compiler Error).
  • The return type is not allowed for the constructors even void also. If we r declaring return type, then the compiler treats it as a method, and hence there is no C.E (Compilation Error) and R.E(RuntimeError).
public class Test {
   void Test() {
      System.out.println("Hai .....");
   }
   public static void main(String arg[]) {
      Test t = new Test();
   }
}

It is legal (But Stupid) to have a method whose name is exactly the same as the class name. Here it was treated as method.

Default Constructor

If we are not writing any constructor, then the compiler always generates the default constructor. If we r writing at least one constructor, then the compiler won’t generate any constructor. Hence every class contains either a programmer-written constructor or compiler-generated default constructor but not both simultaneously.

Prototype of default constructor
  1. It is always a no-arg constructor.
  2. It contains only one–line super();
    This is a call to superclass – constructor. It is a no-argument call only.
  3. The modifier of the default constructor is the same as the class modifier(either public or default).
    The first line inside any constructor must be a call to the superclass constructor(super()) or a call to the overloaded constructor of the same class(this()). If we are not taking anything, then the compiler will always place super().
super() & this() in constructor

We should use as first statement in the constructor. We can use either super or this but not both simultaneously. Outside constructs we can’t use, i.e., we can invoke a constructor directly from another constructor only.

Overloaded Constructor

A class can contain more than one constructor with different arguments. This type of constructor is called an “overloaded constructor.”

public class Test {
   Test() {
      this(10);
      System.out.println("No-arg constructor");
   }
   Test(int i) {
      this(10.5);
      System.out.println("int-arg");
   }
   Test(double d) {
      System.out.println("double-arg")
   }
   public static void main(String arg[]) {
      Test t1 = new Test();
      Test t2 = new Test(10);
      Test t3 = new Test(20.5);
      Test t4 = new Test('a');
      Test t5 = new Test(10l);
   }
}

The inheritance concept is not applicable for the constructor, and hence overriding is also not applicable. Suppose the parent class constructor throws some checked exception. Compulsory, the child class constructor should throw the same checked exception, or its parent otherwise, compile-time error. If the parent class constructor throws an unchecked exception, then the child class constructor is not required to throw that exception.

That’s all about Constructors in Java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!

Constructors in Java
Scroll to top