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.  

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值