Search code examples
javaandroidaide-ide

accessing private variables in JAVA (AIDE)


class a{
    private int i=100;
    a(){
        //this.i=5;
    }
}
class b extends a {
    b( ){
    //super.i=10;
    a a = new a();
    System.out.println(super.i);

}

}
class c {
    public static void main(String [] args){

        b b = new b();
        }}

In the above program i was trying to access private variables with super keyword which i know its not possible but surprisingly in my android mobile using this AIDE (java ide) i was able to access this with super but not directly as i have commented out in the code .i just want to know is that bug with AIDE or i am doing something wrong here.

image


Solution

  • If AIDE gave you the impression you could use super.i in class b and compile it, then yes, that's a bug in AIDE. i has private access in a, and so you cannot use it outside of a. A Java compiler would say:

    b.java:5: error: i has private access in a
        System.out.println(super.i);
                                ^

    In Java, the overwhelming convention is to use an initial upper-case character in class names. You can do what you like in your own code, but when working with others or asking for help, following the conventions is probably better. So A, not a, and B not b (for the classes).