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 final - access modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion 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.
I agree. Here's the link:
subject: final - access modifier
It's not a secret anymore!