• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

immutable

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make the class final make fields private and final

or

Implement Singleton Pattern.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at the discussion

Hope this helps
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable objects are objects that do not change state?? are you agree on this??
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at Effective Java By Joshua Bloch of Item 15
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic