aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "immutable " Watch "immutable " New topic
Author

immutable

Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
As String class is immutable.
Can anyone explain what steps.. or how we can make a class immutable.

Thanks in advance....


Rajeev Kumar
SCJP 6.0 Certified 96%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

make the class final make fields private and final

or

Implement Singleton Pattern.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

look at the discussion

Hope this helps
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Minhaj kaimkhani wrote:Implement Singleton Pattern.


Bad Idea.Singleton is nothing to do with Immutability
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Immutable objects are objects that do not change state?? are you agree on this??
Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Look at Effective Java By Joshua Bloch of Item 15
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Minhaj kaimkhani wrote:Immutable objects are objects that do not change state?? are you agree on this??

Agree. Hint: are String, Integer, Double classes singleton?


http://muhammadkhojaye.blogspot.com/
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Minhaj kaimkhani wrote:Immutable objects are objects that do not change state?? are you agree on this??

You are right Minhaj, but what you said in your previous post is not correct
Minhaj kaimkhani wrote:make the class final make fields private and final
or
Implement Singleton Pattern.

To be immutable, a class doesn't need to implement singleton pattern (in fact that actually doesn't has anything to do with immutable classes) and marking fields final isn't necessary either...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
Ankit Garg wrote:
Minhaj kaimkhani wrote:Immutable objects are objects that do not change state?? are you agree on this??

You are right Minhaj, but what you said in your previous post is not correct
Minhaj kaimkhani wrote:make the class final make fields private and final
or
Implement Singleton Pattern.

To be immutable, a class doesn't need to implement singleton pattern (in fact that actually doesn't has anything to do with immutable classes) and marking fields final isn't necessary either...


Yes Ankit. Singleton Pattern is like one Instance per class. In Singleton you create a Private Constructor, Declare a Static variable of same Class type and provide a Static get method which will create/or point to existing instance of the class. One can access instance of class by Static call to getter method.

But please can some one explain for Immutable if we declare class Final and fields Private and Final makes a class immutable???

Final means no subclass possible means no one can extend that class.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Rajiv Chopra wrote:But please can some one explain for Immutable if we declare class Final and fields Private and Final makes a class immutable???

Fields of an immutable class don't need to be final, there should be no setter methods for the fields, if they only have getter methods and are private, then they cannot be changed once the instance of the class is created. The thread pointed out by seetharaman explains everything very nicely...
 
wood burning stoves
 
subject: immutable