Is it true that the assigned final object field may still be null inside a constructor?
class MyClass {
private final Object obj = new Object();
public MyClass() {
System.out.println(obj); // may print null?
}
}
if yes, isn't this a bug?
As discussed by other answers, no, that can not happen. With an assigned final static field, however, it can.
class MyClass {
private static MyClass myClass = new MyClass();
private static final Object obj = new Object();
public MyClass() {
System.out.println(obj); // will print null once
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With