HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Static Initializers and Uninitializers"
Vol. 3, Issue 5, p. 66

	

Listing 1: Class dependency. Main is dependent on Foo, is dependent on Bar.
  
public class Main {  
  public static void main(String[] astrArgs) {  
    System.out.println("Starting the program");  
    Foo f = new Foo();  
    System.out.println("Created the Foo object");  
    f.xyz();  
  }  

  // static initializer  
  static {  
    System.out.println("Loading class Main");  
  }  
}  

class Foo {  
  static {  
    System.out.println("Loading class Foo");  
  }  

  public void xyz() {  
    Bar bar = new Bar();  
  }  
}  

class Bar {  
  static {  
    System.out.println("Loading class Bar");  
  }  
}  

Listing 2.
  
The implicit dependancy of class Foo on class Bar is caused 
by the fact that the Foo code uses Class.forName() to load class Bar. 
This means the Bar class may be unloaded and reloaded several 
times during the lifetime of the Foo class.  

public class Main {  
  static Foo f = new Foo();  

  public static void main(String[] astrArgs) {  
    System.out.println("Starting the application");  

    Thread t = new Thread(f);  
    t.start();  

    try {  
      while(true) {  
        synchronized(f) {  
          System.in.read();  
          f.notify();  
        }  
      }  
    } catch (Exception e) {  
      // poor exception handling,  
      // for the sake of brevity  
    }  
  }  
}  

class Foo implements Runnable {  
  static {  
    System.out.println(  
        "Loading Foo class");  
  }  

  public Foo() {}  

  public synchronized void run() {  
    while(true) {  
      System.out.println(  
          "Filling memory with " +  
          "1000 useless objects");  

      Class cls = Class.forName("Bar");  
      for(int ii=0 ; ii<1000 ; ii++)  
        cls.newInstance();  

      cls = null;  
      System.gc();  

      try {  
        wait();  
      } catch (InterruptedException ie) {  
      }  
    }  
  }  
}  

class Bar {  
  static {  
    System.out.println(  
        "***Loading Bar class");  
  }  

  public Bar() {}  
}  

Listing 3:
 
A static-only NativeMemory class requires a static uninitializer. 
The static inner class object fulfills that function.  
public class NativeMemory {  
  private static uniniter = new Uninit();  
  ...  

  // Static initializer loads native library,  
  // starts allocating chucks of memory, etc.  
  static {  
    ...  
  }  

  // Public, static only interface  
  public static int alloc(int size) {   // returns handle  }  
  public static void setmem(int handle, byte[]) {...}  
  public static void dealloc(int handle) {...}  
  public static int realloc(int handle, int newsize) {...}  
  ...  

  // Private static inner class, a singleton class,  
  // the singleton object acts as a class uninitializer  
  // for this class  
  private static class Uninit {  
    public Uninit() {}  

    // Called only when NativeMemory class  
    // ready to be unloaded.  
    public void finalize() {  
      // Has access to private static members of the  
      // NativeMemory class. Uses them to  
      // uninitialize the class.  
    }  
  }  
}
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.