Answer the question
In order to leave comments, you need to log in
Why do uninitialized variables already have default values when specifying a constructor?
Here is an example program where I define a constructor
class Test {
int x, y;
Test() {
x = 3;
}
}
class Main {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x + " " + t.y);
}
}
Answer the question
In order to leave comments, you need to log in
Because int is a primitive type. Primitive types have a default value (they are not objects and cannot be null).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question