java "void" and "non void" constructor

26,573

Solution 1

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.

Solution 2

public void class1() is not a constructor, it is a void method whose name happens to match the class name. It is never called. Instead java creates a default constructor (since you have not created one), which does nothing.

Solution 3

Using void in the constructor by definition leads it to not longer be the constructor. The constructor specifically has no return type. While void doesn't return a value in the strictest sense of the word, it is still considered a return type.

In the second example (where you use the void), you would have to do h.class1() for the method to get called because it is no longer the constructor. Or you could just remove the void.

Share:
26,573
Hadi
Author by

Hadi

just like coding!

Updated on July 09, 2022

Comments

  • Hadi
    Hadi almost 2 years

    I wrote this simple class in java just for testing some of its features.

    public class class1 {
        public static Integer value=0;
        public class1() {
           da();
        }
        public int da() {
            class1.value=class1.value+1;
            return 5;
        }
        public static void main(String[] args) {
           class1 h  = new class1();
           class1 h2 = new class1();
           System.out.println(class1.value);
        }
    }
    

    The output is:

    2

    But in this code:

    public class class1 {
        public static Integer value=0;
        public void class1() {
            da();
        }
        public int da() {
            class1.value=class1.value+1;
            return 5;
        }
        public static void main(String[] args) {
            class1 h  = new class1();
            class1 h2 = new class1();
            System.out.println(class1.value);
        }
    }
    

    The output of this code is:

    0

    So why doesn't, when I use void in the constructor method declaration, the static field of the class doesn't change any more?

  • Makoto
    Makoto almost 10 years
    It can be argued that a constructor is a special case of a method that is used to initialize an instance of the object, since it syntactically supports 99% of what a constructor supports.
  • azurefrog
    azurefrog almost 10 years
    Avoiding this sort of confusion is another good reason to follow the method naming conventions in the JLS.
  • chrylis -cautiouslyoptimistic-
    chrylis -cautiouslyoptimistic- almost 10 years
  • Makoto
    Makoto almost 10 years
    @chrylis: Yes, I know this. But in doing AST parsing with Java, I've seen it come across as a method with no return type (hence, constructor).
  • Cerbrus
    Cerbrus almost 8 years
    MyClass x = new MyClass(); x.MyClass(); Perfectly valid.
  • Stephen C
    Stephen C almost 2 years
    The design flaw (if there is one) is that class names and method names are in different namespaces. Or that the Java naming conventions are not part of core Java syntax! But it is too late to fix this. That's why we have tools like FindBugs, etc.