kirk wrote: > [email protected] wrote: > >> On Feb 17, 6:15 am, Alexander Snaps <[email protected]> wrote: >> >> >>> On Sun, Feb 15, 2009 at 7:13 PM, Reinier Zwitserloot >>> <[email protected]>wrote: >>> >>> >>> >>>> I mark any immutable class final, because if it isn't, then you can't >>>> rely on its immutability (any subclass is assignment compatible and is >>>> not neccessarily immutable!). >>>> >>>> >>> While I tend to agree with marking "type" classes as final, I don't believe >>> that a class being immutable is reason enough to mark it final. >>> >>> >> If it's not final, it probably isn't immutable. Take java.io.File. >> Please. >> >> > Marking a class as final is a whole different ball game than marking a > variable as final. For example, marking String as final was a HUGE > mistake IMHO. It prevented people from making some very useful > extensions. It's also responsible for a lot of code bloat. I'm not > against making a class final. That said, to do so because of the need to > mother other developers shouldn't be one of them. > If String wouldn't be final, then its immutability could not be guaranteed, which would mean you
(1) could not copy it in remote conversations but would have to talk to the remote reference, (2) couldn't intern() it, thus having to evaluate a full equals() on each comparison, (3) couldn't do a whole range of such optimizations. They should have stopped and thought a bit longer when they did all of this for String and maybe they would have seen the larger pattern of value objects, but I believe if String wouldn't be final or something equivalent Java wouldn't be where it is since performance would be awful in many situations. The same applies to anything anyone wants to optimize under an assumption of immutability. You need final classes for that. And if you are consequent you also need a JVM not allowing reflection, but that's a whole different story. Peter --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---
