Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

immutable

 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As String class is immutable.
Can anyone explain what steps.. or how we can make a class immutable.

Thanks in advance....
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make the class final make fields private and final

or

Implement Singleton Pattern.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at the discussion

Hope this helps
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minhaj kaimkhani wrote:Implement Singleton Pattern.


Bad Idea.Singleton is nothing to do with Immutability
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Immutable objects are objects that do not change state?? are you agree on this??
 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at Effective Java By Joshua Bloch of Item 15
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic