java “void” and “non void” constructor

In Java, the constructor is not a method. It only has the name of the class and a specific visibility. If it declares that returns something, then it is not a constructor, not even if it declares that returns a void. Note the difference here:

public class SomeClass {
    public SomeClass() {
        //constructor
    }
    public void SomeClass() {
        //a method, NOT a constructor
    }
}

Also, if a class doesn’t define a constructor, then the compiler will automatically add a default constructor for you.

Leave a Comment