/* This class is an example demonstrating that you can code a * class that works with, and without, the presence of other classes. * Use this if you want to take conditionally take advantage of * an 'optional' class, * like javax.* classes that may not be present everywhere. * * http://www.bolthole.com/java/ */ public class parent { boolean have_aux=false; aux testinstance=null; static void debug(String msg){ System.out.println(msg); } public parent(){ // Here is the actual "test" for the aux class. // It doesnt do anything we care about, but // it DOES throw an exception if class not loadable. // Otherwise, with 'new', there is no exception that // we can catch try { Class.forName("aux"); } catch (ClassNotFoundException err){ debug("Note: aux class not available. "); return ; } testinstance=new aux(); have_aux=true; debug("main class has loaded 'aux' class"); } void dostuff(){ debug("In dostuff(). Here is some regular stuff"); if(have_aux){ debug("We have aux. main instance calling 'aux' class"); testinstance.extrastuff(); } else { debug("Sorry, aux class is not loaded. No extra stuff"); } } public static void main(String args[]){ parent myinstance=new parent(); debug("New instance of parent created. Now doing stuff..."); myinstance.dostuff(); } }