On 12/28/05, Diogo Quintela (EF) <[EMAIL PROTECTED]> wrote:

> Non-static inner classes as in your example, despite having an empty
> constructor, are managed internally by java through a constructor with an
> instance to containing class instance (as I believe), so it has direct
> access to the instance variables and methods of its enclosing instance.
>
> A brief lookout into reflection api, doesn't seem to be able to instantiate
> this type of classes; as digester is trying to instantiate that class though
> reflection you must have problems...

Inner classes need an outer class instance for creation. If you for
instance have this class:

public class A
{
  public class B
  {
    ...
  }
}

then you can create an instance of B *outside* of A only with:

A anInstanceOfA = ...
B anInstanceOfB = anInstanceOfA.new B();

If I remember correctly, reflection works similar. The inner class
won't have a no-arg constructor but instead every of its constructors
(even the default one supplied by Java if none is defined)
automatically gets an additional first parameter of the type of the
enclosing class. So accessing the constructor via reflection would be
something like:

Constructor constructor = A.B.class.getDeclaredConstructor(new Class[]
{ A.class});

anInstanceOfB = (A.B)constructor.new Instance(new Object[] { anInstanceOfA });

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to