Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final with object handle

 
umang bhartia
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Herbert Maosa
Ranch Hand
Posts: 289
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Demyanovich
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic