Can a class have no constructor?

If you don't write the constructor explicitly, compiler will generate a no-args constructor by default.

public Maze(){

}    

the above will be included If you don't write the constructor explicitly, compiler will generate a no-args constructor by default.

public Maze(){

}    

the above will be included by the compiler.

for Example check the Byte code for this class:

public class ABC {

}

BYTE CODE:

public class sorting/ABC {

  // compiled from: ABC.java

  // access flags 0x1
  public <init>()V         //Default no-args constructor included by the compiler
   L0
    LINENUMBER 7 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init>()V
    RETURN
   L1
    LOCALVARIABLE this Lsorting/ABC; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1
}

The typical answer to this question is "if you don't declare a constructor, a default constructor is created". That is usually true, but not always. It is possible for a class to have no constructor.

(An important distinction to draw here is that the JVM does not require all class files to have a constructor; however, any class defined in Java does have a default constructor if a constructor is not explicitly declared. This answer is presenting an oddity where an example of the former is created from Java code).

Consider the following code, from this question:

public class Outer
{
    private class Inner {}

    void someMethod()
    {
        Inner inObj = this.new Inner();
    }
}

If you compile this with OpenJDK, you will find 3 class files:

Outer.class
Outer$Inner.class
Outer$1.class

Outer$1 is the most unusual of these: it has literally nothing in it, not even a constructor:

Compiled from "Outer.java"
class Outer$1 {
}

whereas the Inner and Outer classes have the generated constructors:

Compiled from "Outer.java"
class Outer {
  Outer();        <------------- Generated Constructor
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  void someMethod();
    Code:
       0: new           #2                  // class Outer$Inner
       3: dup
       4: aload_0
       5: aconst_null
       6: invokespecial #3                  // Method Outer$Inner."<init>":(LOuter;LOuter$1;)V
       9: astore_1
      10: return
}
Compiled from "Outer.java"
class Outer$Inner {
  final Outer this$0;

  Outer$Inner(Outer, Outer$1);        <------------- Generated Constructor
    Code:
       0: aload_0
       1: aload_1
       2: invokespecial #1                  // Method "<init>":(LOuter;)V
       5: return
}

To be more accurate, the compiler automatically provides a no-args constructor for a class that doesn't have a constructor, this constructor calls the no-args constructor of the superclass, if the superclass doesn't have a no-args constructor, it's an error, if it does, that's fine.

If your class has no explicit superclass, then it has an implicit superclass (Object), which does have a no-args constructor.


It is not required to explicitly define a constructor; however, all classes must have a constructor, and a default empty constructor will be generated if you don't provide any:

public Maze() {
}

See Default Constructor.

Tags:

Java