Java inner class with the same name as other top level class?

Use the fully qualified class name: new com.mypackage.A().

If your classes are in packages, I'd expect that to be the simplest way of achieving this. I don't believe there's any equivalent of the C# global:: for Java, to force this sort of thing. Ultimately though, I'd just try to change the class names to avoid the problem happening in the first place.

That's usually a better approach than using workarounds.

You should provide different package names for your two As so that they are clearly different types, then you can reference them by their full names. For example: my.package.for.toplevel. A outerA = new my.package.for.toplevel.A(); // prints "A outer" // if this is the // package Or even better, use two different names for two distinct classes.

Yep, just use packages or avoid naming them the same from the beginning. I wonder what's your reason to name them the same anyway? To explain your error, it comes from the fact that it tries to access the inner A, but since that class is not declared as static and there's no Main instance available, it can't create a non-static inner A that requires a reference to parent Main instance.

Thanks I forgot about the the fully qualified class name trick. The question was rather theoretical related to SCJP topics (not a real problem). I try to not use such weird constructs :) – bary May 5 at 13:17.

The name of the inner class is said to shadow the name of the top-level class. The top-level class cannot be referenced by its simple name in the scope of the inner class; the top-level class can only be referenced via a qualified name. If the top-level class is in the default package (in which case its canonical name is the same as its simple name), you can still access it via reflection.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions