• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

final - access modifier

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much guys!!! Much appreciated.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic