Hey there,

> AFAIK: you can never create an instance of an inner class from outside
> its parent class. 

Not correct: you can create them from subclasses if they're not private
classes; if they're package protected the subclass must be in the same
package; you can even create them from outside, given an object of the outer
class:

public class A 
{
   public class B 
   {
   }
}

public class Main 
{
   public static void main(String[] args) 
   {
      A a = new A();
      A.B b = a.new B();
   }
}

> I think this is valid for static innerclasses too.

Not correct: you can create public static inner classes from wherever you
want, and restricted access static inner classes (ie protected or package
private) from the allowed places as usual. In the example above, if class B
is static, you can do:

public class Main 
{
   public static void main(String[] args) 
   {
      A.B b = new A.B();
   }
}

> solution: move from innerclass to real class

Agree, simplest and safer.

> May somebody correct me if I m wrong!

You're absolutely right !

Regards,

Simon

Reply via email to