Java: name ambiguity between external and internal methods of a class

Suppose I have:

public class OuterClass() { public class InnerClass { public void someMethod(int x) { someMethod(x); } } public void someMethod(int x) { System.out.println(x); } } 

How to resolve the ambiguity between someMethod() an outer class and someMethod() inner class?

+6
java oop inner-classes ambiguity outer-classes
source share
3 answers

You can access the external using OuterClass.this or call the method with OuterClass.this.method() .

However, as a design point, the separation of name is confusing, to say the least. It would be reasonable if the inner class was an extension or, say, a concrete implementation of an abstract method, but that would be clearer by calling the super.method method. Calling the super method directly (do you think you're going to do this?) Is confusing.

+13
source share

Combine it into an outer class using OuterClass.this.someMethod() :

 public class OuterClass { public class InnerClass { public void someMethod(int x) { OuterClass.this.someMethod(x); } } public void someMethod(int x) { System.out.println(x); } } 
+6
source share

Renaming ambiguities is good practice. Especially if you apply it in an upward and reverse architecture.

+1
source share

All Articles