⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 counted.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.classes;

/** A Java class to demonstrate how static members
  * are defined, initialized, and used.
  */
public class Counted {

   private int value;
   private static int count = 0;  // simple init clause

   /** Default constructor method */
   public Counted() {
      ++count;
      System.out.println( "Creating a "
                          + "Counted object" );
      value = -1;
   }

   /** Class finalizer */
   public void finalize() {
      --count;
      System.out.println( "Destroying a "
                          + "Counted object" );
   }

   /** Method to return the number of objects
     * @return The number of existing objects of
     *    the class
     */
   public static int getCount() {
      return count;
   }

   /** Mutator method to set the value of the object
     * @param newValue The new value for the object
     * @return The updated object
     */
   public Counted setValue( int newValue ) {
      value = newValue;
      return this;
   }

   /** Accessor function to obtain the value of
     * the object
     * @return The value of the object
     */
   public int getValue() {
      return value;
   }

   /** The test method for the class
     * @param args Not used
     */
   public static void main( String[] args )
   {
      Counted a = new Counted();
      Counted b = new Counted();
      Counted c = new Counted();
      System.out.println( "There are " 
                          + Counted.getCount() 
                          + " Counted objects" );
      Counted d = new Counted();
      System.out.println( "There are " 
                          + Counted.getCount()
                          + " Counted objects" );
      a = null;
      System.gc();       // start garbage collection
      System.runFinalization();
      System.out.println( "There are " 
                          + a.getCount()
                          + " Counted objects" );

      /* without the next three method calls, the
       * objects b, c, and d would be picked up in
       * the preceding garbage collection
       */
      b.setValue( 1 );
      c.setValue( 2 );
      d.setValue( 3 );
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -