Java: Class.forName() to retrieve inner class

Sounds trivial, but was not obvious because Eclipse IDE mislead me: How do I get a reference to a inner class by calling Class.forName() ?

package a.b.c;
public class Outer {
   ...
   public static class Inner {
      ...
   }
   ...
}

I first tried to call Class.forName() passing as parameter the fully qualified name generated by Eclipse:

Class innerClass = Class.forName("a.b.c.Outer.Inner");

But that was wrong! After wondering for some time, I found the (obvious solution):

Class innerClass = Class.forName("a.b.c.Outer$Inner");

Looks like as Eclipse “fully qualified” name is not exactly the same fully qualified name that the Java compiler uses to identify inner classes.

One Response to Java: Class.forName() to retrieve inner class

  1. dinocore1 says:

    Thanks this helped

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: