OO Programming with Java


Loading objects

As a complement, reflection can be used to "dynamically" instanciate object and call methods as illustrated below:

try {   
  Class<Catalog>       c = Catalog.class;
  Constructor<Catalog> t = c.getConstructor();
  Object      o = t.newInstance();
  Method      m = c.getDeclaredMethod("loadBinary", Class.forName("java.lang.String"));
  m.invoke(o, "bookstore.ser");
  System.out.println(o);
} catch (Exception e) {
  e.printStackTrace();
}

Exercice: can you draw the class' diagram for Class, Object, Method, ... ?