Some trouble with inner class visibility

Related searches

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.

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.

Nested Classes (The Java™ Tutorials > Learning the Java , Non-static nested classes (inner classes) have access to other members of the As a member of the OuterClass , a nested class can be declared private , public When the Java compiler compiles certain constructs, such as inner classes, Consequently, you may have compatibility issues if you serialize an inner class� The Final modifier lets the inner class access i, it being a local variable inside the method defining the inner class. But as soon as the inner class defines its version of i, the local i at //1 is totally inaccesable (unless you cheat and change it from i to j). I'd love someone to prove me wrong.

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

Java Static Classes Isa Total Mess, "static" doesn't give an inner class external visibility. "public" does. Also, having a class definition for every instance may introduce certain resource use issues. Visibility in UML, which could be public, package, protected and private, allows to constrain the usage of an element in namespaces. It is used with classes, packages, generalizations, element import, package import.

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.

Nested and Inner Classes, Java allows you to define classes inside of other classes. e.g. by the nested curly braces in Java), classes defined as such garner some unique and rather than the compiler flagging the secondary issue of referencing a non-final variable . Visibility declaration is moot because the class has no name FieldType1 f1� Definition and Usage. The visibility property specifies whether or not an element is visible.. Tip: Hidden elements take up space on the page. Use the display property to both hide and remove an element from the document layout!

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
}

Inner class in java, Nested Inner class can access any private instance variable of outer class. An interface can also be nested and nested interfaces have some� Inner class means one class which is a member of another class. There are basically four types of inner classes in java. 1) Nested Inner class 2) Method Local inner classes 3) Anonymous inner classes 4) Static nested classes. Nested Inner class can access any private instance variable of outer class. Like any other instance variable, we can

Inner classes in java: Anonymous inner and static nested class, What is an inner class? Inner class are defined inside the body of another class ( known as outer class). These classes can have access modifier or even. You can use inner classes to implement helper classes such as the one shown in the this example. To handle user interface events, you must know how to use inner classes, because the event-handling mechanism makes extensive use of them. Local and Anonymous Classes. There are two additional types of inner classes.

The outer component creates an instance of the inner component and forwards the calls to the inner component when it needs to leverage some of the functionality exposed by the inner component. From the client's perspective, it never knows that there is an inner component shielded by the outer component that does work for the outer component.

Java inner classes implements encapsulation. Note that inner classes can access outer class private members and at the same time we can hide inner class from outer world. Keeping the small class within top-level classes places the code closer to where it is used and makes the code more readable and maintainable. That's all for java inner class.

Comments
  • private variables can be only modified in the class itself, not from the subclasses.
  • Possible duplicate: outer class variables
  • "the member variable x is not a property of class B": however true that is, I believe the question is really about what is the difference between A.this.x = 10; and b.x = 10;. After all, both are B instances (or is this where the confusion is)?
  • You need to update b instance, not this, you can do it by casting the instance to unsure visibility (since we are in an inner-class). ((A)b).x = 10;
  • It's an inner class not a sub-class.
  • @stridecolossus it's both.
  • Updated the answer @stridecolossus
  • But they are accessible to an inner-class !
  • Both line are not doing the same thing. The first one update b, the second update this...
  • Yes but x = 10 modifies A variable x, while super.x = 10 modifies B variable x inherited from A, right?
  • No, x = 10 is the same as super.x = 10 here. x only exist in A so there is no variable x in B.