There is a code:

class Point { int getX() {return x;} int y=getX(); int x=3; } public static void main (String s[]) { Point p=new Point(); System.out.println(p.x+", "+py); } 

And the result will be: 3 0

But it is not entirely clear how it turned out that y is now equal to 0, not 3

  • 3
    when you get the value for y, x is not yet initialized. For simple types, a default initialization occurs, for int it is 0. You first assign the value y, and after that the value x = 3, while y remains = 0, why should it become = 3? - pavlofff
  • @pavlofff think worth the answer - Vartlok
  • one
    @Vartlok It is not yet clear what the problem is of the author, in my opinion everything is obvious and logical in the code, but the author does not think so. - pavlofff
  • @pavlofff the author clearly does not know how the class fields are initialized, hence such questions, and while you wait, some strange answers have already begun to fall down, which are also zaplusovali. - Vartlok

1 answer 1

 int getX() {return x;} int y=getX(); (при этом х не присвоен) int x=3; (теперь х равен 3)