aspose file tools*
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 Soft Skills 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
Author

final - access modifier

Mayur Ramgir
Greenhorn

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
Hi,

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
Greenhorn

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
System.out.println(obj1.i);
Test1 obj2=new Test1();
obj1=obj2 // complilation error obj1 is final and trying to change obj1 memory.
}
}
Mayur Ramgir
Greenhorn

Joined: Sep 26, 2007
Posts: 8
Thank you very much guys!!! Much appreciated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final - access modifier