load module

Michael Neumann neumann@s-direktnet.de
Sun, 11 Jun 2000 19:52:57 +0200


Hi!


Mark S. Miller wrote:
> At 12:44 AM 6/10/00 , Michael Neumann wrote:
> >Another question:
> >How can I create an instance (or access static's) of an inner class?
> >
> >Example:
> >
> >public class A {
> >    static class B {
> >    }
> >}
> >
> >Here I want to get an instance of A.B .
>
> Not implemented yet, sorry.  On VisualCafe 3.0c using Java 1.1.7a both
> Class.getClasses() and Class.getDeclaredClasses() returns an empty list on
> your example, even when I declare B to be "static public".  Is this a Java
> bug?  In order to implement this feature, I need some way of accessing an
> Inner Class object from dynamic knowledge of its name.  These two methods
of
> Class are the only ways I know of doing so, and I don't know how to make
> them work.  Anyone?

With JDK 1.2.2 this works fine.
I wrote following example to demonstrate this:


import java.lang.reflect.*;

class Outer {
   public static class Inner {
      public Inner() {}
      void test() { System.out.println("Inner::test()"); }
   }
}

public class Test {

   public static void main(String args[]) {

      Outer outer = new Outer();

      Class inner_class = outer.getClass().getClasses()[0];
      Constructor cons[] = inner_class.getConstructors();

      try {

         Outer.Inner inner = (Outer.Inner) cons[0].newInstance(new
Object[0]);
         inner.test();        // prints "Inner::test()"
      }
      catch(java.lang.IllegalAccessException e) {}
      catch(java.lang.InstantiationException e) {}
      catch(java.lang.reflect.InvocationTargetException e) {}

   }
}


----------------


Regards
  Michael