// ObjectClassTests.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-17 File created. // 2005-05-17 Last modification. // Testing the ultimate superclass Object. import java.util.* ; class TestClass implements Cloneable { static int object_counter = 0 ; int object_number ; public TestClass() { object_counter ++ ; object_number = object_counter ; } public void make_speak() { System.out.print( "\n Hi! I'm object number " + object_number ) ; } public TestClass clone() throws CloneNotSupportedException { return ( (TestClass) super.clone() ) ; } public void finalize() { System.out.print( "\n Object number " + object_number + " is being killed!" ) ; } /*** public String toString() { return "TestClass object number " + object_number ; } ****/ } class ObjectClassTests { public static void main( String[] not_in_use ) throws CloneNotSupportedException, InterruptedException { Scanner keyboard = new Scanner( System.in ) ; Integer some_integer_object = 333444 ; String some_string = "AAABBB" ; TestClass some_test_object = new TestClass() ; System.out.print( "\n some_integer_object.hashCode() " + some_integer_object.hashCode() + "\n some_integer_object.getClass() " + some_integer_object.getClass() + "\n" ) ; System.out.print( "\n some_string.hashCode() " + some_string.hashCode() + "\n some_string.getClass() " + some_string.getClass() + "\n" ) ; System.out.print( "\n some_test_object.hashCode() " + some_test_object.hashCode() + "\n some_test_object.getClass() " + some_test_object.getClass() + "\n some_test_object.toString() " + some_test_object + "\n" ) ; Object some_object = some_test_object ; System.out.print( "\n some_object.getClass() " + some_object.getClass() + "\n" ) ; some_object = some_integer_object ; System.out.print( "\n some_object.getClass() " + some_object.getClass() + "\n" ) ; some_object = some_string ; System.out.print( "\n some_object.getClass() " + some_object.getClass() + "\n" ) ; TestClass another_test_object = new TestClass() ; some_test_object.make_speak() ; another_test_object.make_speak() ; TestClass third_test_object = some_test_object.clone() ; third_test_object.make_speak() ; // Let's make the three objects non-referenced objects (i.e. garbage) third_test_object = null ; another_test_object = null ; some_test_object = null ; System.gc() ; // Let's force garbage collector to do its job. // Let's wait that the garbage collector does its job. System.out.print( "\n Press the Enter key to continue ... " ) ; String any_string_from_keyboard = keyboard.nextLine() ; } }