jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static final with object handle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static final with object handle" Watch "static final with object handle" New topic
Author

static final with object handle

umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
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

Joined: May 03, 2000
Posts: 289
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

Joined: Sep 25, 2000
Posts: 173
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
 
subject: static final with object handle
 
Similar Threads
run time constants?
implicit casting
Aquestion about type of variable
static and final
Static final fields and final fields