21

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?

Hadi
  • 1,203
  • 1
  • 10
  • 20

5 Answers5

59

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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 2
    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. – Makoto Jul 25 '14 at 20:12
  • 1
    Avoiding this sort of confusion is another good reason to follow the method naming conventions [in the JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html). – azurefrog Jul 25 '14 at 20:13
  • @Makoto In Java, [a constructor is specifically something that looks a lot like a method but isn't a method](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8). – chrylis -cautiouslyoptimistic- Jul 25 '14 at 20:14
  • @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). – Makoto Jul 25 '14 at 20:15
7

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.

Khary Mendez
  • 1,818
  • 1
  • 14
  • 18
2

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.

Cal Stephens
  • 725
  • 9
  • 20
0

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.

  • `MyClass x = new MyClass(); x.MyClass();` Perfectly valid. – Cerbrus Jun 20 '16 at 10:54
  • 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. – Stephen C Jun 24 '22 at 00:08
-1

The reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the run time.

Here is an article explaining this in greater detail: https://www.quora.com/Why-is-the-return-type-of-constructor-not-void-while-the-return-type-of-a-function-can-be-void

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25
  • This is incorrect. You are conflating the **syntax** of Java constructors with what happens under the hood. They *could* have designed the syntax of Java differently so that constructors were declared as having a return type. They didn't. It was a syntax design decision. And the correct decision was made (IMO), given the way that they wanted Java object construction to work. They needed a way to distinguish methods and constructors at the syntax level. That's what the presence / absence of the return type does. – Stephen C Jun 24 '22 at 00:11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 24 '22 at 20:28