File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Immutable objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Immutable objects" Watch "Immutable objects" New topic
Author

Immutable objects

venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
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

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Plus, override the default serialization method, so the objects can't be changed via serialization.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Apr 29, 2006
Posts: 106
Thanks a lot All for replying, this all information was really useful for me.
Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Immutable objects