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

Immutable objects

 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
In one of the interviews I was asked how to create immutable objects. Also just like String objects, these objects if created without using new , should refer to the same object.
eg.
String s1="aaa";
String s2="aaa"; then s1 & s2 both refer to the same object.
So they asked me how to write a class which follows this property. Obviously I didn know the ans, can anyone tell me how to do it??
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that was a confused question. Strings do some special magic to make your s1==s2. Writing the literal "abc" generates a String object which is "interned" into a string pool. When you reference "abc" again, the compiler sees there is already a "123" in the pool and uses it again.

That would be useless if String was not immutable. You wouldn't want to point to "123" in the pool and later find it says "abc". But the String magic is not part of being immutable. It's an optimization in the compiler.

You can make your own immutables, but you can't make your own magic pool. Immutable just means the state can't change. This will get you close enough for jazz: make all variables private, make no methods that modify variables, and make sure that any non-primitive variables reference objects that are also immutable.

You can defeat the immutability of such classes in Java with the reflection API. Using a workaround to deliberately subvert a design is cheating and is likely to get you just the punishment you deserve.

Does that help?
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plus, override the default serialization method, so the objects can't be changed via serialization.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[vp]: Also just like String objects, these objects if created without using new , should refer to the same object.

It sounds like the interviewer was looking to see if you know (or can figure out) the Flyweight pattern. Java's String class achieves this with extra help from both the compiler and from native code in the JVM (the intern() method). However it's possible to do something similar yourself using just Java - see the example in the link I gave. It's also possible to make this more secure, preventing anyone from using new at all, by making the constructor private, and making the factory a static nested class within the class being flyweighted (in the example, GraphicChar is the class being flyweighted). This in turn makes it difficult to create any alternate implementations of the flyweighted class if you ever need them - that's the tradeoff here between security and extensibility.
[ February 22, 2007: Message edited by: Jim Yingst ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that's one of the least helpful WikiPedia articles I've run into. I have no idea what this means:

In the flyweight pattern, the data has no pointers to the data type methods, because these would consume too much space. Instead, the subroutines are called directly. In some cases, flyweight inheritance is performed by "shift-in" and "shift-out" data markers as a higher-level operation cycles through an array of flyweight data.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I pretty much skipped that part. The subsequent example and Java code are much more usable, and I would assume that at least some of the links lead to other useful resources.
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot All for replying, this all information was really useful for me.
Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic