I wrote this simple class in java just to test some of its functions.
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); } }
Output:
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, when I use void in the declaration of the constructor method, the static field of the class no longer changes?
java constructor void
daren shan
source share