Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Using final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using final " Watch "Using final " New topic
Author

Using final

r suraaj
Greenhorn

Joined: Oct 26, 2008
Posts: 16
Hi.,
my doubt is about final keyword.,

i think a variable whether it is primitive or reference variable ., when it is declared as final variable we can't modify that value.,

but can we assign a final reference variable of an object to another reference variable of the same class?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Final in combination with a primitive means that it's value can never change.
Final in combination with a reference means that the reference can never change.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
r suraaj wrote:Hi.,
my doubt is about final keyword.,

i think a variable whether it is primitive or reference variable ., when it is declared as final variable we can't modify that value.,

but can we assign a final reference variable of an object to another reference variable of the same class?


It depends.

You can do something like this:



But you can't do this:



Once the reference variable is assinged an object, the reference cannot be changed. Note that the object state can change, such as below:



SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using final
 
Similar Threads
assigning a final value to a method local variable
class instance variables modifying
About synchronized code block.....
Legal arguments in switch case statements
clarification on final methods