3
class Host {
    int x=2;

    class Helper {
        int x = 7;
    }

    public static void main(String[] args){
        Host ho = new Host();
        Helper he = ho.new Helper();
        System.out.println(ho.x);
        System.out.println(he.x);

    }
}

So here I'm getting the expected output

2
7

Now I wanted to ask that, say, I want to access ho's x from he.

I.e. I want something here which will print me 2 through the Helper object he:

System.out.println(???);

I know there's no use of such a thing, I just want to clarify my concept of nested classes. I figure that this should be possible, because the Helper object he is sort of 'binded' to the Host object ho. Since he is not possible without ho. From inside the Helper class I can do System.out.println(Host.this.x); and it works. I can't figure out how to do it from inside main.

2
  • You can't, there is no instance of Helper "living" into Host ho Commented Dec 4, 2012 at 9:58
  • The question has been answered over here - stackoverflow.com/questions/56974/… Commented Jul 13, 2018 at 15:29

4 Answers 4

1

As already pointed out by other answers you can't. The reason lies in the way this is defined in the JLS #15.8.3

The keyword this may be used only in the body of an instance method, instance initializer, or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.

And since you can only access the enclosing instance with this (cf JLS #15.8.4), that can only be done within the inner class:

It is a compile-time error [ to call C.this] if the current class is not an inner class of class C or C itself.

Sign up to request clarification or add additional context in comments.

Comments

1

Back in the time, in old versions of Java, you used this$0 as the way to access the outer instance instead of Host.this. The specification has changed but the field is still available through reflection :

Field this$0 = he.getClass().getDeclaredField("this$0");
Host host = (Host) this$0.get(he);
System.out.println(host.x);

I don't know any other way (apart modifying the Host class to add a getX or getHost method).

Now, why isn't this access available without reflection ? I can see two possible reasons :

  • they forgot
  • this access from outside the instance breaks the encapsulation

1 Comment

@assylias I suppose it was considered it would go against encapsulation and maybe would lead to new questions regarding field access rights. It also looks a lot like how closure protect variables in some other languages.
0

Basic Java concept, Host class can access inner class variable x where as vice versa not possible. You can do like what @Nikita Beloglazov is saying. But directly using variable, not possible

4 Comments

Hm. Interesting. So how do I do it in the other direction? How do I access he's x from ho?
You can't as @RC in your comment saying
Oh, so I can't do it either way? I can only access their variables though their independent objects?
So what does this mean, as you said this in your reply : Host class can access inner class variable x
0

You can create method in inner class that returns outer class:

class Helper {
    int x = 7;

    public Host outer() {
        return Host.this;
    }
}

// In main;
System.out.println(he.outer().x);

It is similar to accessing x inside Helper but more general.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.