Aaron
Aaron

Reputation: 999

Can a class have no constructor?

This is a piece of code as an example, after this rest are just methods (look at bottom for maze class). So when this is instantiated, using

Maze labyrinth = new Maze();

and

System.out.println (labyrinth);

This will print out the grid array. Is this legit? I thought all classes needed constructors how does it print out the 2-d grid array?

Maze Class:

public class Maze
{
    private final int TRIED = 3;
    private final int PATH = 7;
    private int[][] grid = { {1,1,1,0,1,1,0,0,0,1,1,1,1},
                             {1,0,1,1,1,0,1,1,1,1,0,0,1},
                             {0,0,0,0,1,0,1,0,1,0,1,0,0},
                             {1,1,1,0,1,1,1,0,1,0,1,1,1},
                             {1,0,1,0,0,0,0,1,1,1,0,0,1},
                             {1,0,1,1,1,1,1,1,0,1,1,1,1},
                             {1,0,0,0,0,0,0,0,0,0,0,0,0},
                             {1,1,1,1,1,1,1,1,1,1,1,1,1} };

    public String toString ()
    {
        String result = "\n";
        for (int row = 0; row < grid.length; row++)
        {
            for (int column=0; column < grid[row].length; column++)
            result += grid[row][column] + "";
            result += "\n";
        }
        return result;
    }

}

Upvotes: 64

Views: 115491

Answers (7)

Abdelwahab El Sayed
Abdelwahab El Sayed

Reputation: 1

if you mean that you want no one to make an Instance of your class directly i think you can make a constructor set it's access modifier to private and you only can make an instance of this class by a public static method which have access to this private constructor

Upvotes: -1

Andy Turner
Andy Turner

Reputation: 140319

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
}

Upvotes: 7

Pravallika
Pravallika

Reputation: 57

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, then the Java compiler will create a default constructor with an empty argument.

Upvotes: 0

PermGenError
PermGenError

Reputation: 46408

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
}

Upvotes: 15

Maroun
Maroun

Reputation: 95958

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.

Upvotes: 9

jahroy
jahroy

Reputation: 22692

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.

Upvotes: 4

mellamokb
mellamokb

Reputation: 56769

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.

Upvotes: 74

Related Questions