aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final Objects ..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final Objects ....." Watch "final Objects ....." New topic
Author

final Objects .....

Shailesh Vohra
Greenhorn

Joined: Feb 15, 2010
Posts: 9
Hi all,

If references are marked final,

final Object o1 = new Object();
Object o2 = new Object();

we cant do this,

o1 = o2; //This is wrong.

but surely we can change the state of o1 (i.e change the value of instance variables for o1).


Now suppose i want to restrict even changing the state of the object o1.

How can we achieve this..??

Any help is appreciated.



Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi Shailesh Vohra,
UseCodeTags
And
I think this is called immutable objects
Might be achieved by,
In every method that changes the state of the object:
Take a copy of the object and make the required changes, then return that newly created object. (Referred String class documentation)
Or
Do not provide any method that changes the state of the object. So only at the time of creation we can set the value for the instance variables of the object.


A.A.Anbarasu
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
Do a search on Singleton design pattern.


Rajeev Trikha (SCJP 6)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Rajeev Trikha wrote:Do a search on Singleton design pattern.

Singleton pattern has nothing to do with the original question...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shailesh Vohra
Greenhorn

Joined: Feb 15, 2010
Posts: 9
i did not understand your explanation Anbarasu Aladiyan.
Can one explain again...?
I just have to make my Objects also final....How to achieve it.?
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Shailesh Vohra wrote:i did not understand your explanation Anbarasu Aladiyan.
Can one explain again...?
I just have to make my Objects also final....How to achieve it.?

What does meant by changing object's state?
It means changing the object's instance variable. So first make sure instance variables are not visible by putting private modifier.
And do NOT provide any setter methods. Because changing the instance a variable means changing the state of the object.
And in all the methods that changes the instance variable (for example subtract, add...) of the object.
First takes a copy of current object and make the changes in instance variables of new object. Then return that newly created object.
I think better you can refer string class documentation (source code).
Also be sure you know very well about immutable objects
Here is the concat() of string class:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: final Objects .....