• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how do I make a class immutable?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.. i am new to this forum... i have a doubt.. please answer me immediately..

my question is how can i make a class immutable...??
[ June 12, 2007: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Writing an immutable class is generally easy but there can be some tricky situations. Follow the following guidelines:

1.A class is declared final (i.e. final classes cannot be extended).
public final class MyImmutable { � }

2.All its fields are final (final fields cannot be mutated once assigned).


3.Do not provide any methods that can change the state of the immutable object in any way � not just setXXX methods, but any methods which can change the state.

4.The �this� reference is not allowed to escape during construction from the immutable class and the immutable class should have exclusive access to fields that contain references to mutable objects like arrays, collections and mutable classes like Date etc by:

�Declaring the mutable references as private.
�Not returning or exposing the mutable references to the caller (this can be done by defensive copying)
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i have a doubt.. please answer me immediately..

First of all Ease up.

how can i make a class immutable...??


In Java, a class is immutable if no methods are provided to mutate class level variables. Basically its easier to make a class immutable than mutable. Just don't provide any Mutator methods.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

Please go through this excellent article which speaks about

  • Mutable Vs Immutable Objects
  • How to make a class/object Immutable?


  • Hope this will help you to a good extent.
    [ June 12, 2007: Message edited by: Raghavan Muthu ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic