Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a class have no constructor?

Tags:

java

People also ask

Does every class has a constructor?

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors.

What happens if no constructor?

you wouldn't be able to create objects/instances of classes, basically: you would only be able to use primitive datatypes. Even you don't write any constructor, a default constructor exists there. So you can still create objects without passing any parameters.

Can a class have no constructor C++?

If your class has no constructors, C++ will automatically generate a public default constructor for you. This is sometimes called an implicit constructor (or implicitly generated constructor). The Date class has no constructors.

Which class Cannot have a constructor?

A constructor is a method called when a class is instantiated, and an abstract class cannot be instantiated.


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.


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
}

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.


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
}

If you don't specify a constructor, a default constructor will be generated by the compiler.

However, any member variable that is not initialized when it's declared will be null.

In other words, if you don't assign a value to grid (like you do in your example) it will be null.

Your example works fine because you happen to assign a value to grid immediately upon declaring it.