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
|