java - Make instance of Singleton Class object eligible for GC -


i have class jaxbreader hold unmarshalled xml file using jaxb generated classes. have used singleton design need not unmarshall file again , again. object of class (precisely unmarshalled xml) needed initialize enum having 8 constants. constructor of enum constants use singleton object required part of xml.

after enum initialized, don't need objetc of jaxbreader in system. how can achieve this?

i read here can call setter assign null static singelton instance don't want externally. want that, instance assigned null automatically after enum initialized.

i using java 1.7

one option of within static initializers of enum. keep static field within enum itself, write private static method contents of file, reading if necessary, , @ end of enum initialization - in static initializer block - set null:

public enum foo {     value1, value2, value3;     private static jaxbreader singlereader;      static {         singlereader = null; // don't need more     }      private foo() {         jaxbreader reader = getreader();         // use reader     }      private static jaxbreader getreader() {         // don't need worry thread safety, of         // done in single thread initializing enum         if (singlereader == null) {             // happen once             singlereader = new jaxbreader(...);         }         return singlereader;     } } 

this way enum knows reader singleton - can still create new jaxbreader whenever externally, useful testing.

(i'm nervous enum initialization needing external resource start with, can see may hard avoid.)


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -