Diablo3000
Diablo3000

Reputation: 67

Some trouble with inner class visibility

I'm facing java inner class and I'm having some trouble with outer variables visibility.

class A {
    private int x = 5;
    class B extends A{
        public void fun(B b){
            b.x = 10; //here
        }
    }
}

Why I can't do something like this (see the "here" tag) if there are no visibility restrictions between inner and outer class? I'm really not understanding these rules.

Upvotes: 1

Views: 76

Answers (4)

AxelH
AxelH

Reputation: 14572

This one is quite special. Inner class can access the private field of the outerclass. This can be shown with the fun(A) method

class A {
    private int x = 5;
    class B extends A{

        public void fun(A a){
            a.x = 5; //Valid since we are in the same class (inner-class can access private field)
        }
}

Now, if you have a B parameter, this is a bit different because it will try to use the inheritence instead of the outer-inner link :

public void fun(B b){
    b.x = 10; //Access using inheritence, not OK for private field
    ((A)b).x = 10; //Casting into A to be able to access the private field (just like fun(A).
}

Note that this is a problem because you update a reference receive in parameter, you can update the current inner instance easily

public void fun(){
    x = 10; //OK : access to A.x from inner class
    super.x = 10; //OK : same

    this.x = 10; // KO : trying to access a private variable from B scope
}

Upvotes: 0

abj1305
abj1305

Reputation: 665

Modifier private cannot be accessed in sub classes.

class A {
    private int x = 5; // The issue is in `private` in this line
    class B extends A{
        public void fun(B b){
            b.x = 10; //here
        }
    }
}

If you remove the modifier private and change it to default, public OR protected, you will be able to access the variable.

Please go through the link for better understanding.

UPDATE:

Remove extends keyword (Now Class B will not be a sub class but only an inner class), and the variable x will only be accessed using this OR super keyword. The usage is elaborated at link

Upvotes: 1

lonecloud
lonecloud

Reputation: 413

you need know this three privateprotected,default,publicaccess specifiers

  1. private variables only can be modified in the itself class.not include subclass
  2. default variables can be modified in same package.
  3. protected variables can be modified in subclass and in same package and itself
  4. public variables can be modified any where.

you can saw this on this link

beside.if you use this example ,this can modified your variables with usingsuperspecifiers to access your visiable

class A {
    private int x = 5;
    class B extends A{
        public void fun(B b){
            b.x = 10; //here error
            super.x=1;//this is ok
        }
    }
}

this b.x = 10; //here error this problem is b is a parm of a methods not a member-variable or a super class variable.

Upvotes: 0

stridecolossus
stridecolossus

Reputation: 1561

In your example the member variable x is not a property of class B so b.x = 10 makes no sesne, hence the error, it's nothing to do with visibility rules. Trying x = 10 works fine, which is a short-cut for A.this.x = 10 or super.x = 10.

Upvotes: 2

Related Questions