• 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

How to make a class immutable (like String)?

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever we perform any operation on the class's objects, the old object remains and a new object is created. Can we do this?

Thanks.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-- If you don't write any mutators, the class is immutable.
-- You can't "perform any operation" on the instance of an immutable class.
-- Creating a new instance is up to you as the programmer.

db
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the old object remains and a new object is created



The string literal pool is not part of the immutable behaviour of a class. (Have a read of this). Immutable just means you can't change the value of something, it doesn't imply that such an object will be stored differently.

The string literal pool is part of the JVM. You can't get objects of other types to use it. But you can (sort of) implement simmilar logic if you control access to the construction of a new instance of your object. Have a google for "factory patterns" and you'll get a lot of information on the subject.
[ June 16, 2008: Message edited by: Paul Sturrock ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arjun Reddy:
Whenever we perform any operation on the class's objects, the old object remains and a new object is created. Can we do this?

Thanks.



I may be wrong, but my interpretation of this is that the OP is thinking of methods such as substring which return a new String rather than modifying the existing one.
If I'm right, then you can certainly do something similar in your own class. All you need to do in these methods is create a new instance of your class and then set the state of this new instance based on the state of the existing instance and the required modifications and then return a reference to this new instance.
 
reply
    Bookmark Topic Watch Topic
  • New Topic