public class Test{
int i;
Test(){
System.out.println(i);
}
public static void main(String[] args){
Test obj=new Test();
}
}
Output=0
P.S: I am naive in Java so this question could be very silly. I am expecting your support. Thanks
public class Test{
int i;
Test(){
System.out.println(i);
}
public static void main(String[] args){
Test obj=new Test();
}
}
Output=0
P.S: I am naive in Java so this question could be very silly. I am expecting your support. Thanks
Static/Instance fields which are not initialized will be set to a default value by the compiler.
The table bellow indicates the default value for data types:
+--------------------------+----------------------------+
| Data Type | Default Value (for fields) |
+--------------------------+----------------------------+
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| char | '\u0000' |
| String (or any object) | null |
| boolean | false |
+--------------------------+----------------------------+
For more information, you can see the doc here
See, every Instance-Variable or Class-Variable initialized by it's default value
In case instance-variable is some Reference type then it will be always assigned with it's default value by
null
So, here in your case int is primitive type whose default value is 0(zero) that's the reason you get 0 in out-put.