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??
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
[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
Joined: Jan 29, 2003
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.
Joined: Jan 30, 2000
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.
Joined: Apr 29, 2006
Thanks a lot All for replying, this all information was really useful for me. Thanks again