Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Constraints:

I have a maven source code generator that I wrote that is creating POJO classes from some data files that have nested namespaces. I want each namespace to be nested as an inner class. In some cases out of my control I end up with inner classes that are the same simple name as the outermost class.

All the classes must be public scope as this is for a type safe wrapper over something like a properties file, but hierarchical..

I can't change the names otherwise I am changing the names meaning and the namespace that is enclosing data.

Given than I have the following code:

public class A
{
    public class B
    {
        public class A
        {

        }
    }
}

Inner classes should append the name of the outer class to form a unique namespace such as A$B$A.class, I haven't found a valid reason for this not to compile.

Is there any trick to get this to compile?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

No. From the JLS section on class declarations:

It is a compile-time error if a class has the same simple name as any of its enclosing classes or interfaces.

Note: I somehow managed to miss this on my first pass through looking for an explicit rule. Check the edit history if you want the tortuous way I got here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...