KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > InstantiationException

java.lang
Class InstantiationException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.lang.InstantiationException
All Implemented Interfaces:
Serializable
See Also:
Top Examples, Source Code, Class.newInstance()

public InstantiationException()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1100]Use reflection to instantiate singletons
By Anonymous on 2004/11/05 17:41:37  Rate
//Use reflection to instantiate singletons  
  
  
 import java.util.HashMap; 
 import org.apache.log4j.Logger; 
  
  
 public class Singleton  {  
    private static HashMap map = new HashMap (  ) ; 
    private static Logger logger = Logger.getRootLogger (  ) ; 
  
  
    protected Singleton (  )   {  
       // Exists only to thwart instantiation 
     }  
    public static synchronized Singleton getInstance ( String classname )   {  
       Singleton singleton =  ( Singleton ) map.get ( classname ) ; 
  
  
       if ( singleton != null )   {  
          logger.info ( "got singleton from map: " + singleton ) ; 
          return singleton; 
        }  
       try  {  
          singleton =  ( Singleton ) Class.forName ( classname ) .newInstance (  ) ; 
        }  
       catch ( ClassNotFoundException cnf )   {  
          logger.fatal ( "Couldn't find class " + classname ) ;     
        }  
       catch ( InstantiationException ie )   {  
          logger.fatal ( "Couldn't instantiate an object of type " + classname ) ;     
        }  
       catch ( IllegalAccessException ia )   {  
          logger.fatal ( "Couldn't access class " + classname ) ;     
        }  
       map.put ( classname, singleton ) ; 
       logger.info ( "created singleton: " + singleton ) ; 
  
  
       return singleton; 
     }  
  } 


public InstantiationException(String s)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags