Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Design with dynamic extension

How dynamic extension works in Java and how to use it in your designs

  • Print
  • Feedback

Page 6 of 7


// In file dynaext/ex2/Cat.java
public class Cat { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { Class c = Class.forName(args[0]); Rodent myToy = (Rodent) c.newInstance(); myToy.scurry(); }
}
// In file dynaext/ex2/Rodent.java
public class Rodent { public void scurry() { System.out.println("Rodent scurrying"); }
}
// In file dynaext/ex2/Mouse.java
public class Mouse extends Rodent { public void scurry() { System.out.println("Mouse scurrying"); }
}


In this version of class Cat, the first argument to the application is passed to forName(). (Note that this is an example of a type name being passed around as a String, a telltale sign of an application that uses dynamic extension.) Assume that the application is invoked with the argument "Mouse". This String will be passed to forName(), which will attempt to load the type.

Figure 4 shows what the name spaces look like after scurry() is invoked on the Mouse object. Once again, only one name space, that of the primordial loader, exists. This looks the same as that of the previous application that didn't use dynamic extension.

This is because forName() follows the same rule the JVM follows when it resolves a symbolic reference in the constant pool of a class file. forName() will use the same class loader to load the requested class that loaded the class that contained the code that invoked forName(). Because Cat, the class that invoked forName(), was loaded by the primordial loader, forName() will use the primordial loader to load Mouse.

Figure 4. forName() follows the rule

Class java.lang.Class

To understand dynamic extension, you need to know a bit about class java.lang.Class. For every type that a JVM loads, it creates an instance of class java.lang.Class to "represent" the type to the rest of the application. Given a reference to a type's Class instance, you can access the information that the JVM extracted from the class file that defined the type.

For example, you can find out whether the type is a class or an interface. If it is a class, you can find out if it is abstract and get a reference to the Class instance for its superclass. You can get an array of Class instances for it directly implements, if any. If the SecurityManager allows it, you can even find out what fields and methods the class contains and invoke the methods.

The most important aspect to dynamic extension is that if you have a reference to a Class instance for a non-abstract class, you can instantiate an instance of the class by invoking newInstance() on the Class object. If forName() is successful at loading the type (or if the type was already loaded into the current name space), forName() will return a reference to the Class instance that represents the type. By invoking newInstance() on the Class object returned by forName(), the Cat application creates an instance of class Mouse.

Interacting with a dynamically loaded class

When Cat invokes scurry() on the Rodent reference myToy, the JVM does a dynamic bind and executes Mouse's implementation of scurry(). The application prints out "Mouse scurrying.". This is true even though Cat didn't know about Mouse at compile time. At compile time, Cat knew only about Rodent. This is the most common way to interact with a dynamically loaded class: to invoke methods defined in a supertype (superclass or superinterface) of the dynamically loaded class on an instance of that class.

  • Print
  • Feedback

Resources
  • Bill's next book is Flexible Java http://www.artima.com/flexiblejava/index.html
  • An complete online reprint of Chapter 7, "The Linking Model," of Bill's book Inside the Java Virtual Machine http://www.artima.com/insidejvm/linkmod.html
  • The handout and slides for Bill's "Dynamic Extension in Java" talk http://www.artima.com/javaseminars/modules/DynaExt/index.html
  • Bill recently returned from his European bike trip. Read about it at http://www.artima.com/bv/travel/bike98/index.html
  • The discussion forum devoted to the material presented in this article http://www.artima.com/flexiblejava/fjf/dynaext/index.html
  • Links to all previous Design Techniques columns http://www.artima.com/designtechniques/index.html
  • Recommended books on Java design http://www.artima.com/designtechniques/booklist.html
  • A transcript of an e-mail debate between Bill Venners, Mark Johnson (JavaWorld's JavaBeans columnist), and Mark Balbe on whether or not all objects should be made into beans http://www.artima.com/flexiblejava/comments/beandebate.html
  • Object orientation FAQ http://www.cyberdyne-object-sys.com/oofaq/
  • 7237 Links on Object Orientation http://www.rhein-neckar.de/~cetus/software.html
  • The Object-Oriented Page http://www.well.com/user/ritchie/oo.html
  • Collection of information on OO approach http://arkhp1.kek.jp:80/managers/computing/activities/OO_CollectInfor/OO_CollectInfo.html
  • Design Patterns Home Page http://hillside.net/patterns/patterns.html
  • A Comparison of OOA and OOD Methods http://www.iconcomp.com/papers/comp/comp_1.html
  • "Object-Oriented Analysis and Design MethodsA Comparative Review" http://wwwis.cs.utwente.nl:8080/dmrg/OODOC/oodoc/oo.html
  • Patterns discussion FAQ http://gee.cs.oswego.edu/dl/pd-FAQ/pd-FAQ.html
  • Patterns in Java AWT http://mordor.cs.hut.fi/tik-76.278/group6/awtpat.html
  • Software Technology's Design Patterns Page http://www.sw-technologies.com/dpattern/
  • Previous Design Techniques columns http://www.javaworld.com/topicalindex/jw-ti-techniques.html