Could you please explain me this strange behaviour?
public class Car {
private int wheels;
public Car(int wheels) {
System.out.println("Before: " + wheels); // prints 3 before initialisation
this.wheels = wheels;
System.out.println("After: " + wheels); // prints 3
}
public static void main(String[] args) {
Car car = new Car(3);
}
}
If you run this code, you it will print twice 3
, instead of 0
, and just then, after initialisation of the field wheels
, 3
.