ASCII silly question, Get a silly ANSI.
"I'm not back." - Bill Harding, Twister
Yes, and if you build a fire under a refrigerator, you can use it to heat food!
Originally posted by Ernest Friedman-Hill:
Yes, and if you build a fire under a refrigerator, you can use it to heat food!
SCJP, SCWCD
Tony Morris
Java Q&A (FAQ, Trivia)
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Renu Radhika:
The reason why its told that string is immutable is because the String class is final.
What does immutable means? How is an Immutable object created?
if you build a fire under a refrigerator, you can use it to heat food
Originally posted by Renu Radhika:
Thats what I meant.If you want to change the behaviour of a particular class we have to subclass it.But String class is final so we cant subclass it.Also in the API provided by sun its clearly mentioned that Strings are constants.So may be the private variable which holds the String value will be a final variable which cant be changed.
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
Changing the behaviour of a class has nothing to do with mutability.
Originally posted by Greg Charles:
That's not completely true. A class must be declared final to be (strongly) immutable. That is, class finality is a necessary though not sufficient condition of immutability. If you could subclass String, then you could change the bahavior of a method, say toString(), so that the String would seem mutable. Through polymorphism, the user of your String subclass might think he was just using a normal String, but could possibly see it demonstrating mutable characteristics.
A weakly immutable class would just declare certain methods to be final, and those would be the only ones guaranteed to be immutable.
Tony Morris
Java Q&A (FAQ, Trivia)
Make a class immutable by following these guidelines :
always construct an object completely, instead of using a no-argument constructor combined with subsequent calls to setXXX methods do not provide any methods which can change the state of the object in any way - not just setXXX methods, but any method which can change state
ensure no methods can be overridden - make the class final, or use static factories and keep constructors private make fields final if the state of a mutable object field is "owned" by the native class, and the intention is to allow direct access to the field only from within that native class, then, when the field "crosses the interface" (as in a get or set method, or in the constructor itself), then a defensive copy must be made, in order to maintain encapsulation. if the state of a mutable object field is not "owned" by the native class, then a defensive copy of the object field is not necessary
The ability to subtype classes is a consequence of a language defect
Originally posted by jiju ka:
Can somebody explain the logic behind this statement.
I believe the contrary is true.
Inheritance is prevented by declaring a class as final. In the nature (real world objects) inheritance is not prohibited.
[ November 30, 2005: Message edited by: jiju ka ]
Originally posted by Greg Charles:
The ability to subtype classes is a language defect? I'm not following your logic there. Be that as it may, Java and all languages that permit inheritence suffer this defect. Therefore immutable classes should be final.
From Java Practices:
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
[Tony]: ...(one of which, for example, is that time is linear, therefore, the assertion may well fall apart at the relative speed of light).
Commander, we're entering a region of space with an unusually high bogon flux. Raising shields...
Typical sarcasm and failure to acknowledge a relatively sound approach to reasoning. /ignore
[Tony]: an immutable type is one where a contractual operation is exposed that when invoked causes some other observable side effect on some other contractual operation.
Sounds like you just defined a mutable type. Though you refer to immutability several times later too, so I'm really not sure what the intent here was.
Oops - I meant to add a "not" in there somewhere.
[Tony]: I find this reasoning (make no mistake, I do in fact understand the orthodox reasoning thoroughly) to be so loose, that it falls apart under stringent analysis.
OK. Were you going to provide some reasoning that doesn't fall apart? The thread of what you were saying has gotten quite murky by this point...
Agreed, no less murky than the quoted literature. I reserve the right not to, just like you do.
Going back:
[Tony]: immutable classes cannot exist
Are you saying that Strings don't exist? Strings aren't classes? Strings aren't immutable? The last seems most likely, though we've already addressed this earlier in the thread. Would you also argue that refrigerators don't exist?
Tony Morris
Java Q&A (FAQ, Trivia)
Correct, an immutable String does not exist. An immutable String contract does, however. Are you able to invoke an operation on a String instance that can be observed through some other operation?
Originally posted by Ram kumar:
stuart,
plz go thru the link and decide who is in dark?
http://getahead.ltd.uk/blog/joe/2005/05/26/1117108773674.html