This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final - access modifier 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final - access modifier" Watch "final - access modifier" New topic

final - access modifier

Mayur Ramgir

Joined: Sep 26, 2007
Posts: 8
I found following statement in SCJP 5.0 Study guide;

"A reference variable marked final can never be changed, but the object it refers to can be modified."

Can anybody explain in detail?

Thanks in advance..
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585

Let's suppose that you have the following:

This means when you say:

You will get a compilation error, which means that reference itself is final not the state of the object.

And let's suppose that SomeClass has a property named x, this means that you can do the following:

Without any problems.

Again, a final reference means that you can not refer to another object in the heap, but, you can change the state of the object that this final reference is refering to!

Best of luck ...

Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well,i am not sure but still try
to explain the above
with a small example as below
consider for example there are two classes
class A and class B

i hope it helps you to some extent!!!
[ September 26, 2007: Message edited by: dhwani mathur ]
sarada chellu

Joined: Sep 25, 2007
Posts: 17
I think the below Example hepls you better

class Test1
int i=10;
public static void main(String args[])
final Test1 obj1=new Test1();
obj1.i=20; //works fine! since you are changing the value of i but not obj1 memory
Test1 obj2=new Test1();
obj1=obj2 // complilation error obj1 is final and trying to change obj1 memory.
Mayur Ramgir

Joined: Sep 26, 2007
Posts: 8
Thank you very much guys!!! Much appreciated.
jQuery in Action, 2nd edition
subject: final - access modifier
Similar Threads
about final variable
private final method
What is the Prototype for Main method in java
static final array