Default constructor in Java

Java 8Object Oriented ProgrammingJava Programming

Java compiler automatically creates a default constructor (Constructor with no arguments) in case no constructor is present in the java class. Following are the motive behind a default constructor.

  • Create the Object

  • Call the super class constructor()

  • Initialize all the instance variables of the class object.

Example

 Live Demo

public class Tester {
   int a;
   String b;
   public static void main(String[] args) {

      //Default constructor
      //is called to create a new object
      Tester t = new Tester();
      //print the default values
      System.out.println(t.a);
      System.out.println(t.b);
   }
}

Output

0
null
raja
Published on 13-Aug-2018 13:02:57
Advertisements