Java: duplicate method signature in nested class

Viewed 798

Hopefully this question hasn't already been asked. I've had a look around but haven't found a similar post.

Experimenting in Java I've noticed that there is no restriction on having duplicate method signatures in a nested class, which seems counter-intuitive.

For example, if I create class A containing a method with the signature int x() and later add a nested class B containing an identical method, the compiler seems to have no problem with it. My initial assumption was that it would complain that x is already defined. Perhaps I'm missing something obvious that explains why this is allowed?

class A {
    int x() {
        return 1;
    }

    class B {
        int x() {
            return 2;
        }
    }
}

Subsequently, is there any way to access class A's method x from within the scope of class B, or is it permanently hidden by the method x of the local scope?

Edit: I appreciate that the core of the question is the same as this post, however, I was more interested in understanding why this behaviour is allowed as it wasn't immediately clear to me.

10 Answers

Where a class is defined doesn't matter so much. Keep in mind, in the end you have

class A { int x()

and

class A.B { int x()

Two (almost) independent classes. The only relationship that we have here is that any instance of B needs an "enclosing" instance of A to which it belongs (because it is a non-static inner class).

And of course, you can access the "enclosing" A "stuff" from within B, for example using A.this.x().

This should be available somewhere in the JLS - but ultimately it boils down to scope. Each of them has a different scope - thus the compiler does not complain.

Its scope, it's similar to have an instance variable called foo and then a local one in the method called foo as well.

Its worth reading about scope in java

Why would the compiler complain? They are two different classes, they just happen to be nested.

Subsequently, is there any way to access class A's method x from within the scope of class B, or is it permanently hidden by the method x of the local scope?

A.this.x()

At first method signature is not the combination of return type and method name it is method name and parameters.

if you call x() it will run x() inside the B

A.this.x(); it will run x() in A

Inner class in java can access all the members (i.e variables and methods) including private one, but outer class can not access member of inner class directly.

To access x() method of class B inside class A you can either create B's instance and call it or call like this A.this.x();

To access x() method outside class A you can do something like this:

B b = a.new B();
b.x();

If you are using non-static nested class (inner class) then it wouldn't be able to access to x method of B class from other classes. But if you are using static nested class you will be able to access that method from other classes. Example:

public class A {

    int x() {
        new A.B().x();
        return 1;
    }

    static class B {
        int x() {
            new A().x();
            return 2;
        }
    }
}


public static void main(String[] args) {
        A a = new A();
        a.x();
        B b = new A.B();
        b.x();
}

I hope this example answering your question... ☺

B is nested inside A. According to scope rules we can do the following:

class A {
    int x() {
        return 1;
    }

    class B {
        int x() {
            return 2;
        }

        int xOfA(){
            return A.this.x();
        }
    }

    public static void main(String[] args) {
        final A objA = new A();
        final B objB = objA.new B();
        System.out.println(objA.x());
        System.out.println(objB.x());
        System.out.println(objB.xOfA());
    }
}

that is because B is visible from an instance of A. Moreover B can reference methods in the containing class through their full paths.

For example, if I create class A containing a method with the signature int x() and later add a nested class B containing an identical method, the compiler seems to have no problem with it. My initial assumption was that it would complain that x is already defined. Perhaps I'm missing something obvious that explains why this is allowed?

When you define something in a nest scope which is otherwise in an outer scope, this is hiding. What is important is that -: The version of a method that is executed will NOT be determined by the object that is used to invoke it. In fact it will be determined by the type of reference variable used to invoke the method

Subsequently, is there any way to access class A's method x from within the scope of class B, or is it permanently hidden by the method x of the local scope?

So as it is clear from above we can access it using class name, eg A.x

Hope this helps!!

Related