16
votes

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?

4
That second one isn't a constructor. It's just a method that happens to share a name with its class.murgatroid99
Because it's no longer a constructor.Oliver Charlesworth
the answer on this similar question Why do constructors in java not have a return type gives a great reason.Doswell
its amazing that you wrote answer just before i can refresh the page ; tnx all,Hadi

4 Answers

51
votes

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.

7
votes

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.

1
votes

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.

0
votes

This is arguably a design flaw in Java.

class MyClass {

   // this is a constructor
   MyClass() {...}

   // this is an instance method
   void MyClass() {...}

 }

Perfectly legal. Probably shouldn't be, but is.

In your example, class1() is never getting called, because it's not a constructor. Instead, the default constructor is getting called.

Suggestion: familiarize yourself with Java naming conventions. Class names should start with uppercase.