Week9 - Garbage Collection and the finalize ( ) Method...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Garbage Collection and the finalize ( ) Method Since objects are dynamically allocated by using the new operator, you might be wondering how such objects are destroyed and their memory released for later reallocation. In some languages, dynamically allocated objects must be manually released by use of a delete operator. Java takes a different approach; it handles deallocation for you automatically. When no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object is reclaimed. Garbage collection only occurs sporadically during the execution of your program. Sometimes an object will need to perform some action when it is destroyed. To handle such situations, Java provides a mechanism called finalization . Finalization allows you to define specific actions that will occur when an object is just about to be reclaimed by the garbage collector. To add a finalizer to a class, you simply define the finalize( ) method. At Java run-time, this method is called whenever a corresponding object is about to be recycled. The finalize( ) method has this general form: protected void finalize( ) { // finalization code goes here } It is important to understand that finalize( ) is only called just prior to garbage collection. It is not immediately called when an object goes out- of-scope. Therefore, you do not know when (or if) finalize( ) will be executed.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overloading Methods In Java, it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism. When an overloaded method is invoked, Java uses the type and / or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and / or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
Background image of page 2
// Demonstrate method overloading class OverloadDemo { void test ( ) { System.out.println("No Parameters"); } // Overload test for one integer parameter void test (int a) { System.out.println("a:" + a); } // Overload test for two integer parameters void test (int a, int b) { System.out.println("a and b:" + a + " " + b); } // Overload test for a double parameter double test (double a) { System.out.println("double a:" + a); return a * a; } class Overload { } public static void main(String args[ ]) { OverloadDemo ob = new OverloadDemo ( ); double result; // call all versions to test ( ) ob.test( ); ob.test(10); ob.test(10,20); result = ob.test(123.2); System.out.println("Result of ob.test(123.2):" + result);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

Week9 - Garbage Collection and the finalize ( ) Method...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online