CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
Java doesn't have the C++ concept of a destructor, a method that is automatically called when an object is destroyed. The reason is probably that in Java the practice is simply to forget about objects rather than to destroy them, allowing the garbage collector to reclaim the memory as necessary.
Often this is fine, but there are times when your class might perform some activities during its lifetime that require cleanup. As mentioned in Chapter 4, you can't know when the garbage collector will be called, or if it will be called. So if you want something cleaned up for a class, you must write a special method to do it explicitly, and make sure that the client programmer knows that they must call this method. On top of this, as described in Chapter 9 (exception handling), you must guard against an exception by putting such cleanup in a finally clause.
Consider an example of a computer-aided design system that draws pictures on the screen:
//: CADSystem.java
// Ensuring proper cleanup
import java.util.*;
class Shape
void cleanup()
class Circle extends Shape
void cleanup()
class Triangle extends Shape
void cleanup()
class Line extends Shape
void cleanup()
public class CADSystem extends Shape
void cleanup()
public static void main(String[] args) finally
}
Everything in this system is some kind of Shape (which is itself a kind of Object since it's implicitly inherited from the root class). Each class redefines Shape's cleanup( ) method in addition to calling the base-class version of that method using super. The specific Shape classes Circle, Triangle and Line all have constructors that "draw," although any method called during the lifetime of the object could be responsible for doing something that needs cleanup. Each class has its own cleanup( ) method to restore non-memory things back to the way they were before the object existed.
In main( ), you can see two keywords that are new, and won't officially be introduced until Chapter 9: try and finally. The try keyword indicates that the block that follows (delimited by curly braces) is a guarded region, which means that it is given special treatment. One of these special treatments is that the code in the finally clause following this guarded region is always executed, no matter how the try block exits. (With exception handling, it's possible to leave a try block in a number of non-ordinary ways.) Here, the finally clause is saying "always call cleanup( ) for x, no matter what happens." These keywords will be explained thoroughly in Chapter 9.
Note that in your cleanup method you must also pay attention to the calling order for the base-class and member-object cleanup methods in case one subobject depends on another. In general, you should follow the same form that is imposed by a C++ compiler on its destructors: First perform all of the work specific to your class (which might require that base-class elements still be viable) then call the base-class cleanup method, as demonstrated here.
There can be many cases in which the cleanup issue is not a problem; you just let the garbage collector do the work. But when you must do it explicitly, diligence and attention is required.
There's not much you can rely on when it comes to garbage collection. The garbage collector might never be called. If it is, it can reclaim objects in any order it wants. In addition, implementations of the garbage collector in Java 1.0 often don't call the finalize( ) methods. It's best to not rely on garbage collection for anything but memory reclamation. If you want cleanup to take place, make your own cleanup methods and don't rely on finalize( ). (As mentioned earlier, Java 1.1 can be forced to call all the finalizers.)
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 610
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved