【Java学习】- access specifiers

Note that compiled code is often placed in a different directory than source code, but the path to the compiled code must still be found by the JVM using the CLASSPATH.

Public, protected, and private
If you don't provide an access specifier, it means "package access".
The default access has no keyword.
All the other classes in the current package have access to that member, but to all the classes outside of this package, the member appears to be private.

Package access allows you to group related classes together in a package so that they can easily interact with each other.

The class controls the code that has access to its members.

The only way to grant access to a member is to:

  1. Make the member public. Then everybody, everywhere, can access it.
  2. Give the member package access by leaving off any access specifier, and put the other classes in the same package. Then the other classes in that package can access the member.
  3. An inherited class can access a protected memeber as well as a public member, but not private members.
  4. Provide "accessor/mutator" methods(also known as "get/set" methods) that read and change the value.

Everything is about the management of space & time.

Don't make the mistake of thinking that Java will always look at the current directory as one of the starting point for searching.
If you don't have a '.' as one of the paths in you CLASSPATH, Java won't look there.

posted @ 2022-02-16 11:53  易点灵通  阅读(45)  评论(0)    收藏  举报