This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes class Immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "class Immutable" Watch "class Immutable" New topic
Author

class Immutable

sreejith panampilly
Ranch Hand

Joined: Jun 15, 2006
Posts: 49
how can make a class Immutable?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Very simply, don't provide any way to modify any of the member variables. It's that simple. Make sure they're all private, and preferably final, and don't change them after the constructor.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: class Immutable
 
Similar Threads
Math class immutable
Java
How to make a class immutable??????????
Is the Math class considered immutable?
equals() and ==