Java - Class Constructor and Finalizer 090905

 

This chapter is concerning with following tips:

1.        how the calling sequence of class and superclass constructors
子類與父類對象構造器調用順序研究

2.        how the calling sequence of class and superclass finalizers
子類與父類清理器調用順序研究

3.        how and why override public method toString() of Object
如何和爲何覆蓋Object類對象的toString()方法

4.        how to override protected method finalize() of Object
如何覆蓋Object類對象的受保護方法finalize()

5.        how to call method gc() of System
如何直接調用System類對象的垃圾清理器

6.        purpose of @override
@override
有何用途

 

Referred to:

JDK 6 Documentation

Java How to Program by H. M. Deitel

 

1. First is superclass’s onstructor, then is the remainder of the class itself

When an object of a subclass is instantiated, the super class’s default constructor, or rather no argument constructor, should be called to do any necessary initialization of the super class instance, except an explicit call to some super class constructor (via the super reference). Then the remainder of the subclass constructor’s body executes.

 

 

2. Finalize method calling sequence between class and its superclass

Correspondingly, subclass finalize method should (if overriding finalize) invoke the superclass finalize method (as its last action) to ensure that all parts of an object are finalized properly.

 

 

3. Object.toString()

3.1 Public string toString()

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

3.2 Reason of overriding toString() of Object

Usually we can override method toString() of Class Object so that it can be used to put out more meaning messages during our debugging process of simple code, just like following demo program.

 

 

4. Object.finalize()

protected void finalize()

It’s called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.

 

 

5. System.gc()

 

public static void gc()

Calling the gc method (Run the garbage collector) suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

The call System.gc() is effectively equivalent to the call:

Runtime.getRuntime().gc()

 

6. @override

public @interface Override

Override is an annotation [ˌænəuˈteiʃən] type (Annotation type declarations are similar to normal interface declarations which is an at-sign ‘@’ precedes the ‘interface’ keyword) meaning that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

表示一個方法聲明打算重寫超類中的另一個方法聲明。如果方法利用此註釋類型進行註解但沒有重寫超類方法,則編譯器會生成一條錯誤消息。

Since: 1.5

 

-----------------------------------------------------------------------------------------

Related testing classes include:

  • Point class extends from Object
  • Circle class extends from Point class
  • main class

 

  1. // Point.java
  2. public class Point extends Object {
  3.     protected int x, y;
  4.     // no-argument constructor
  5.     public Point ()
  6.     {
  7.         x = 0;
  8.         y = 0;
  9.         System.out.print( "Point constructor: " + this);
  10.     }
  11.     // constructor
  12.     public Point( int a, int b )
  13.     {
  14.         x = a;
  15.         y = b;
  16.         System.out.print( "Point constructor: " + this);
  17.     }
  18.     // finalizer
  19.     protected void finalize() // overriding Object protected method finalize()
  20.     {
  21.         System.out.print( "Point finzlizer: " + this);
  22.     }
  23.     //convert the point into a String representation
  24.     @Override 
  25.     public String toString()  // overriding Object public method toString()
  26.     { return "[" + x + ", " + y + "]"; }
  27. }
  28.  
  29.  // Circle.java
  30.  public class Circle extends Point {  // inherits from Point
  31.     protected double radius;
  32.     // no-argument constructor
  33.     public Circle() {
  34.         //implicit call to superclass constructor here
  35.         radius = 0;
  36.         System.out.println("Circle constructor: " + this);
  37.     }
  38.     // constructor
  39.     public Circle(double r, int a, int b)
  40.     {
  41.         super(a, b);
  42.         radius = r;
  43.         System.out.println("Circle constructor: " + this);
  44.     }
  45.     // finalizer
  46.     protected void finalize()
  47.     {
  48.         System.out.println("Circle finalizer: " + this);
  49.         super.finalize();
  50.     }
  51.     //convert the circle to a string
  52.     public String toString()
  53.     {
  54.         return "Center = " + super.toString() + "; Radius = " + radius;
  55.     }
  56. }
  57.  
  58. // Main.java
  59. public class Main {
  60.     /**
  61.      * @param args the command line arguments
  62.      */
  63.     public static void main(String[] args) {
  64.         Circle circle1, circle2;
  65.         circle1 = new Circle(4.5, 40, 29);
  66.         circle2 = new Circle(10, 3, 4);
  67.         circle1 = null;
  68.         circle2 = null;
  69.         System.gc();    // call the garbage collector
  70.     }
  71. }
  72.  

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章