Java Constructors

Java Constructors are main part of a Java Class. Constructor is called when an object is created. It is a special method that initializes an object. Usually, constructors assign initial values to the object variables.

The name of constructor is same as the name of class. Constructor should not have any explicit return type. It may or may not have parameters. A constructor cannot have the keywords abstract, static, final, or synchronized.

The syntax for defining a constructor is:

Here, class is the keyword, and ClassName is the name of class and the name of constructor.

Example

The below code shows a class and its constructor:

class Example {
Example() {
 // Constructor body}
}

A constructor ca have private, public or protected access type. The object of a class having private constructor must be initialized within the same class otherwise compiler gives error. The object of a class having public constructor can be initialized within or outside the same class.

class Example {
  private String language;
  // constructor
  Example() {
    System.out.println("Constructor is called:");
    language = "Java";
  }

  public static void main(String[] args) {

    // an object of the Example class
    Example obj = new Example();
    System.out.println("The language is " + obj.language);
  }
}

The output of the above code after compilation is: Constructor is called: The language is Java

Types of Constructors

There are following types of Java Constructors:

  • No Arguments Constructor
  • Parameterized Constructor
  • Default Constructor

No Arguments Constructor

A java constructor that has no arguments is a no arguments constructor. It can have private, public or protected access type. The below example shows a class containing no arguments constructor.

class Example {
  int var;
  // constructor
  private Example() {
    var = 2;
    System.out.println("Constructor is called:");
  }

  public static void main(String[] args) {

    // an object of the Example class
    Example obj = new Example();
    System.out.println("The object variable is " + obj.var);
  }
}

The output of the above code after compilation is: Constructor is called: The object variable is 2 The object variable is 2

Parameterized Constructor

A parameterized java constructor has parameters. A constructor can have one or more parameters. For example, the below code shows a class having parameterized constructor. The constructor takes one argument.

// Create a class
class Example {
  String language;
  // parameterized constructor
  private Example(String lang) {
    language = lang;
    System.out.println("The programming language is:" + lang);
  }

  public static void main(String[] args) {

    // objects of the Example class
    Example obj1 = new Example("Java");
    Example obj2 = new Example("Python");
  }
}

The output of the above program after compilation is: The programming language is: Java The programming language is

Note that the parameter is passed while object initialization for parameterized constructor. If no argument is passed while object creation, then in this case compiler gives an error.

Default Constructor

Default java constructor is created when user creates no constructor in a class. The compiler creates a default constructor having no arguments on its own. The default constructor is created during the execution of the program on run time. The purpose of default constructor is to initialize all the uninitialized variable values to their default values.

For example, the below code shows a class having no argument. example of constructor is below

// Create a class
class Example {
  int a;
  boolean b;
  String c;
  int d = 3;

  public static void main(String[] args) {
    // default constructor is called when object is created
    Example obj = new Example();

    System.out.println("Default Values are:");
    System.out.println("a = " + obj.a);
    System.out.println("b = " + obj.b);
    System.out.println("c = " + obj.c);
    System.out.println("Initialized Values are:");
    System.out.println("d = " + obj.d);
  }
}

Note that only the values of uninitialized variables is set to their default values. However, the value assigned to the initialized variable is not changed. The output of the above code after compilation is: Default Values are Default Values ar

A constructor can not be overridden but it can be overloaded. A constructor can not be over ridden but can be over load