The private keyword, on the other hand, means that no one can access that element except you, the creator of the type, inside methods of that type.
I don’t really understand what’s going on in the highlighted text.
Perhaps a code illustration will help
public MyClass {
private int someField = 1;
public boolean isFieldZero() {
return someField == 0;
}
}
public MyOtherClass {
public void test() {
MyClass c = new MyClass();
System.out.println(c.someField); // <<< ERROR
}
}
"I" wrote the type MyClass
. "I" can access the private
field someField
inside the method isFieldZero
... or any other method of that type that "I" choose to write. "Nobody else" can. And neither can "my" second class MyOtherClass
.
OK so the quoted text is a bit loose in the way that it is conflating the code (MyClass
) with the code's author1(me). That is potentially misleading, and perhaps it was what is confusing you. Some textbook authors use that style of writing to try to engage with the readers of their books. It shouldn't be taken literally.
In reality, the Java compiler is going to allow and restrict access by Java code not people.
1 - ... or authors, considering that a given class may be modified by many people over the time that the application, etc containing the class is maintained.