Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About imutable class

 
Pradyumna Khadanga
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the interviewer asked me "How to write a custom Imutable class ?"
For this what is the exact answer ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your answer?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What is your answer?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An immutable class?
 
Pradyumna Khadanga
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friend I answered him that
Just make all the fields private and don't write any method in that class which has capability to change the state of that object .
and other wise you can make the class singleton.
Thanks to reply me
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. There is no need to use the singleton pattern at all.
All the fields should be final.
The class should be final so it cannot be extended and the subclass altered.
There should be no "set" methods.
Any "get" methods which return primitives or immutable reference types can be unchanged.
Any "get" methods which return mutable reference types should return a defensive copy.
Any methods which might change the state of the object should instead create a new object with the altered state.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradyumna Khadanga wrote:Hi friend I answered him that
Just make all the fields private and don't write any method in that class which has capability to change the state of that object .
and other wise you can make the class singleton.
Thanks to reply me


Umm, no. As Campbell said, the Singleton pattern doesn't really have anything to do with immutability. A good example of an immutable class is String. String clearly is not a singleton.

John.
 
Pradyumna Khadanga
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer ,it will give me a lots of idea . Since i am new to java , I put this Q here .
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be the same answer in other languages.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradyumna Khadanga wrote:One of the interviewer asked me "How to write a custom Imutable class ?"
For this what is the exact answer ?


The exact answer is: Don't make it mutable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic