I am creating an empty Java class and compiling it, will any constructor be created

564 views Asked by At

I am creating an empty Java class and compiling it, will any constructor be created as it is being compiles successfully

Class ABC{

}

2

There are 2 answers

0
Ionut Sinca On BEST ANSWER

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

So, the compiler will put the default constructor for you if you won't put any constructors.

So your class will be like:

class ABC {

    public ABC() {

    }
}
0
HatsuPointerKun On

According to Java Documentation ( Providing Constructors for Your Classes ):

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.

It will create a default constructor if no constructor is defined

Also, if we compile your ABC class and decompile the bytecode generated, we will see this code :

public class ABC {
    public ABC() {
    }
}

So the compiled version have a default constructor