how2examples.com
Home - Introduction to Java Programming

Java Constructors

When an object is created using the new keyword the constructor of the class is invoked. If a class does not have any explicitly defined constructors then the compiler automatically adds a default constructor. A default constructor takes no arguments, is publicly accessable and simply invokes the default constructor of the superclass. As the default constructor is added autimatically it is not a requirement to explicilty define a constructor for clases you wish to create instances of.

Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      ClassWithNoConstructorDefined o = new ClassWithNoConstructorDefined();
      System.out.println(o);
   }
}

class ClassWithNoConstructorDefined {
}
Command to compile Example.java:
javac Example.java
Command to run Example:
java Example
Output from running Example:
ClassWithNoConstructorDefined@19821f

A constructor does not have a return type and must have the same name as the class it is defined in.

Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      ClassWithOneConstructor o = new ClassWithOneConstructor();
      System.out.println(o);
   }
}

class ClassWithOneConstructor {
   ClassWithOneConstructor() {
      System.out.println("ClassWithOneConstructor constructor");
   }
}
Command to compile Example.java:
javac Example.java
Command to run Example:
java Example
Output from running Example:
ClassWithOneConstructor constructor
ClassWithOneConstructor@19821f

Overloading Constructors

You can define any number of constructors for a class - each with different argument lists (in the same way that methods can be overloaded). A constructor can invoke another constructor in the same class by using this().

Invoking Superclass Constructors

Before the constructor of a class is run the constructor of all its superclasses are run. A constructor can specify which constructor of its immediate superclass is invoked by using super().

Contents of ConstructorCompilationErrorExample.java:
public class ConstructorCompilationErrorExample {
   public static void main(String[] args) {
      new B();
   }
}

abstract class A {
   A(int i) {
   }
}

class B extends A {
}
Command to compile ConstructorCompilationErrorExample.java:
javac ConstructorCompilationErrorExample.java
Result of trying to compile ConstructorCompilationErrorExample.java:
ConstructorCompilationErrorExample.java:12: cannot find symbol
symbol  : constructor A()
location: class A
class B extends A {
^
1 error
Contents of ConstructorExample.java:
public class ConstructorExample {
   public static void main(String[] args) {
      new C(1);
   }
}

abstract class A {
   A() {
      System.out.print("A()");
   }
}

class B extends A {
   B(int i, boolean b) {
      System.out.print(" B(int, boolean)");
   }
}

class C extends B {
   C(int i) {
      this(i, false);
      System.out.print(" C(int)");
   }

   C(int i, boolean b) {
      super(i, b);
      System.out.print(" C(int, boolean)");
   }
}
Command to compile ConstructorExample.java:
javac ConstructorExample.java
Command to run ConstructorExample:
java ConstructorExample
Output from running ConstructorExample:
A() B(int, boolean) C(int, boolean) C(int)

A constructor can only contain at most one super() or this() and, if present, it must be the first statement in the constructor.