commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Dudziak <tom...@gmail.com>
Subject Re: [digester] instantiationException if using internal classes ?
Date Wed, 28 Dec 2005 15:23:07 GMT
On 12/28/05, Diogo Quintela (EF) <diogo.quintela@ef.pt> 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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message