Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

About imutable class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your answer?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An immutable class?
 
Pradyumna Khadanga
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75676
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75676
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be the same answer in other languages.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic