Accessing private field in Java -
i don't understand why can access private int i outside of class while it's private.
public class fish { private int = 1; public static void main(string[] args) { fish k = new fish(); k.i = 2; // possible } }
your main method part of fish class isn't it?
the following doesn't work:
public class fish { private int = 1; } class reptile{ public static void main(string[] args) { fish k = new fish(); k.i = 2; // compiler error. } }
Comments
Post a Comment