开发者

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

开发者 https://www.devze.com 2023-03-03 06:30 出处:网络
I have question related to Java inner classes. Is there a way to access top level class A from top level class Main that define inner class A?

I have question related to Java inner classes.

Is there a way to access top level class A from top level class Main that define inner class A?

Below is sample code demonstrating the problem:

class A {   // Outer Class A
  { 
    System.out.println("A outer");
  }
}

class B {   // Outer Class B
  { 
    System.out.println("B outer"); 
  }
}

public class Main { 
  class A {  // Inner Class A
    {
      System.out.println("A inner");
    }
  }

  public void newA() {      
    class A {   // Local Class A
      { 
        System.out.println("A local");
      }
    }
    new A();
  }

  public static void main(String[] args) {
    new Main().newA();  // prints "A local"
    new Main().new A(); // prints "A inner"
    //new A();    // compiler error: No enclosing instance of type Main is Accessible.
    new B();      // but this works开发者_开发问答 and prints "B outer" 
  }
}


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.


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.


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.


Assumed the classes are in the default package. To resolve naming conflict in this case needs either rename the conflicting classes or use the named package. Then use FQCN to resolve the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消