A final variable points to an instance. I wanted to see what would happen if I changed the reference of the object to null. I am very surprised that there is no exception nor "is null" printed out. It is as if the line of code a = null;
has been ignored.
output:
myFoo.name? haha
public class TryJava {
class InnerFoo {
public InnerFoo(String name) {
super();
this.myName = name;
}
String myName;
boolean isStarted;
}
InnerFoo a = new InnerFoo("haha");
final InnerFoo myFoo = a;
void bar() {
a = null; // IGNORED???
System.out.println("myFoo.name? " + (myFoo != null ? myFoo.myName : " is null "));
}
public static void main(String[] args) {
TryJava tj = new TryJava();
tj.bar();
}
}