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

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -