java - Why is the access to a private field not forbidden? -


For my study at the university, I am compelling some ugly Java basics to work, such as work without encapsulation The main method is to do the same class etc. (I do not want to open a discussion on a Java style guide, I just want to clarify that I will not write anything outside the university)

I have a behavior that I can not understand for myself: / P>

  public class person {// field personal interval age; Public static zero main (string [] args) {person foo1 = new person (); Foo1.age = 40; Println (foo1.age); }}  

Why does this piece of code compile and run without error? How is it possible that I can use private sector? Strange behavior due to being the main method in the same class?

Because of the static method main class person Member and thus can access any private sector or methods in person .

What are you worried about? Can a person write a class and then access those methods from their class?

If you are going to be worried about anything, then be concerned that you can enter any private area using square reflection, but very useful things It is also necessary for this.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -