This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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