File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static final with object handle" Watch "static final with object handle" New topic

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.
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.
I agree. Here's the link:
subject: static final with object handle
It's not a secret anymore!