Hi every 1 can any one help me out from the difference between final and static final when used with Object handle. I know that if a variable is declared as final its value remains constant, and when a variable is declared as static final it means that it is a class constant, but is it same with the Object handle? For eg what is the diff between these two: 1. final Value v1 = new Value(); 2. static final Value v1 = new Value(); please help me out of this problem. Thank u.
Joined: May 03, 2000
Static varibale applies to the whole class as you very well state. On the other hand, when the modifier final is applied to an object handle, or class so to speak, it means that that class can not be extended, that is, you can not create subclasses out of it. Herbert.
Joined: Sep 25, 2000
You included the following two lines: final Value v1 = new Value(); static final Value v1 = new Value(); You know that declaring a member static means that it belongs to the class, not any one instance of the class. Declaring a member variable final means that it's value is fixed; it's a constant. When a reference is declared final, as you have done with v1, the reference is fixed. You would not be able to assign another reference to v1, whether via simple assignment of another existing reference or via construction. You can, however, still modify the state of the object referred to by v1 via method calls on v1. So, above, neither reference can be changed once it's set via the construction. And, the first one is an instance member, while the second one is a class member. Craig