U
U
UNy2018-11-08 16:13:23
Java
UNy, 2018-11-08 16:13:23

Fields of java inner classes?

public class parcel1 {
    class contest{
        private int i=1;
        int getI(){return i;}
    }

    class destination{
        private String s;
        destination(String s){
            this.s = s;
        }

        String getS() {
            return s;
        }
    }

    void info(){
        contest c = new contest();
        System.out.println(c.getI());
        System.out.println(c.i);
        destination d = new destination("hello");
        System.out.println(d.getS());
    }

    public static void main(String[] args) {
        parcel1 p = new parcel1();
        p.info();
    }
}

Why do I see the ci variable in the info method if it is declared as private? (java 8)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2018-11-08
@UNy

Because the class has access to all fields and methods of nested classes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question