[ Pobierz całość w formacie PDF ]
.In addition,place a boolean flag in the class to indicate whether the object has beencleaned up so that finalize( ) can check for “the deathcondition” (see Chapter 4).[ Add Comment ]Theresponsibility of finalize( ) can only be to verify “the deathcondition” of an object for debugging.(See Chapter 4.) In specialcases, it might be needed to release memory that would not otherwise be releasedby the garbage collector.Since the garbage collector might not get called foryour object, you cannot use finalize( ) to perform necessarycleanup.For that you must create your own “cleanup” method.In thefinalize( ) method for the class, check to make sure that the objecthas been cleaned up and throw a class derived from RuntimeException if ithasn’t, to indicate a programming error.Before relying on such a scheme,ensure that finalize( ) works on your system.(You might need tocall System.gc( ) to ensure this behavior.)[ Add Comment ]Ifan object must be cleaned up (other than by garbage collection) within aparticular scope, use the following approach: Initialize the object and, ifsuccessful, immediately enter a try block with a finally clausethat performs the cleanup.[ Add Comment ]Whenoverriding finalize( ) during inheritance, remember to callsuper.finalize( ).(This is not necessary if Object isyour immediate superclass.) You should call super.finalize( ) as thefinal act of your overridden finalize( ) rather than thefirst, to ensure that base-class components are still valid if you need them.[ Add Comment ]Whenyou are creating a fixed-size container of objects, transfer them to anarray—especially if you’re returning this container from amethod.This way you get the benefit of the array’s compile-time typechecking, and the recipient of the array might not need to cast the objects inthe array in order to use them.Note that the base-class of the containerslibrary, java.util.Collection, has two toArray( ) methods toaccomplish this.[ Add Comment ]Chooseinterfaces over abstract classes.If you know something isgoing to be a base class, your first choice should be to make it aninterface, and only if you’re forced to have method definitions ormember variables should you change it to an abstract class.Aninterface talks about what the client wants to do, while a class tends tofocus on (or allow) implementation details.[ Add Comment ]Insideconstructors, do only what is necessary to set the object into the properstate.Actively avoid calling other methods (except for finalmethods) since those methods can be overridden by someone else to produceunexpected results during construction.(See Chapter 7 for details.) Smaller,simpler constructors are less likely to throw exceptions or cause problems.[ Add Comment ]Toavoid a highly frustrating experience, make sure that there is only oneunpackaged class of each name anywhere in your classpath.Otherwise, thecompiler can find the identically-named other class first, and report errormessages that make no sense.If you suspect that you are having a classpathproblem, try looking for.class files with the same names at each of thestarting points in your classpath.Ideally, put all your classes withinpackages.[ Add Comment ]Watchout for accidental overloading.If you attempt to override a base-classmethod and you don’t quite get the spelling right, you’ll end upadding a new method rather than overriding an existing method.However, this isperfectly legal, so you won’t get any error message from the compiler orrun-time system—your code simply won’t work correctly.[ Add Comment ]Watchout for premature optimization.First make it work, then make itfast—but only if you must, and only if it’s proven that there is aperformance bottleneck in a particular section of your code.Unless you haveused a profiler to discover a bottleneck, you will probably be wasting yourtime.The hidden cost of performance tweaks is that your code becomes lessunderstandable and maintainable.[ Add Comment ]Rememberthat code is read much more than it is written.Clean designs make foreasy-to-understand programs, but comments, detailed explanations, and examplesare invaluable.They will help both you and everyone who comes after you.Ifnothing else, the frustration of trying to ferret out useful information fromthe online Java documentation should convince you.[ Add Comment ][85]Explained to me by Andrew Koenig.[ ][ ][ ][ ][ ]Last Update:05/21/2001
[ Pobierz całość w formacie PDF ]