I’m wondering if final truly “freezes” its reference, forcing its underlying object to co-exist with any re-assignments made to the original reference via a different path e.g.
class ThingHolder{
Thing thing;
}
class Thing{
int i=5;
Thing(int i){
this.i=i;
}
}
public static void main(String[] args){
ThingHolder thingHolder = new ThingHolder();
thingHolder.thing = new Thing(5);
final Thing aFinalReference = thingHolder.thing;
thingHolder.thing = new Thing(6); //will this now coexist with "aFinalReference"?
//...
}
Will aFinalReference now persist as in independent object, despite no longer being a part of thingHolder, and continue refering to the original Thing (i.e. the one whose int is currently 5), regardless of whatever now happens to thingHolder?
All that
finalreally means here is that once you assignaFinalReference, you cannot reassign it.To answer your question of what will reference what, we’ll just step through the code:
At the end of
main():thingHolder.thingwill point to “Thing6”aFinalReferencewill point to “Thing5”