Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

About imutable class

 
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 ?
 
Marshal
Posts: 67426
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your answer?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:What is your answer?

 
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
 
Marshal
Posts: 69889
278
  • 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.
 
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
Marshal
Posts: 69889
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be the same answer in other languages.
 
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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic