Chapter 6. Access Control -- Thinking in Java

1) Java provides access specifiers to allow the library creator to say what is available to the client programmer and what is not. The levels of access control from “most access” to “least access” are publicprotected, package access (which has no keyword) and private.

 

2) A package contains a group of classes, organized together under a single namespace. A library is a group of these class files. Each source file usually has a public class and any number of non-public classes, so there’s one public component for each source file. If you want to say that all these components (each in its own separate .java and .class files) belong together, that’s where the package keyword comes in. If you use a package statement, it must appear as the first non-comment in the file.

 

3) When you create a source-code file for Java, it’s commonly called a compilation unit (sometimes a translation unit). Each compilation unit must have a name ending in .java, and inside the compilation unit there can be a public class that must have the same name as the file (including capitalization, but excluding the .java file name extension). There can be only one public class in each compilation unit; otherwise, the compiler will complain. If there are additional classes in that compilation unit, they are hidden from the world outside that package because they’re not public, and they comprise “support” classes for the main public class.

 

4) When you compile a .java file, you get an output file for each class in the .java file. Each output file has the name of a class in the .java file, but with an extension of .class. Thus you can end up with quite a few .class files from a small number of .java files. A working program is a bunch of .class files, which can be packaged and compressed into a Java ARchive (JAR) file (using Java’s jar archiver). The Java interpreter is responsible for finding, loading, and interpreting these files.

 

5) When you say: package mypackage;
you’re stating that this compilation unit is part of a library named mypackage. Put another way, you’re saying that the public class name within this compilation unit is under the umbrella of the name mypackage, and anyone who wants to use that name must either fully specify the name or use the import keyword in combination with mypackage. (Note that the convention for Java package names is to use all lowercase letters, even for intermediate words.)

 

6) Collecting the package files into a single subdirectory solves three problems:

    a. The .class files under one package won't get cluttered

    b. Creating unique package names and finding those classes that might be buried in a directory structure someplace. This is accomplished by encoding the path of the location of the .class file into the name of the package.

    c. Resolving the package name into a directory on your machine,  .class files can be located and loaded easily and quickly by JVMs.

 

7)  The Java interpreter proceeds as follows. First, it finds the environment variable CLASSPATH. CLASSPATH contains one or more directories that are used as roots in a search for .class files. Starting at that root, the interpreter will take the package name and replace each dot with a slash to generate a path name off of the CLASSPATH root. This is then concatenated to the various entries in the CLASSPATH. That’s where it looks for the .class file with the name corresponding to the class you’re trying to reference. (It also searches some standard directories relative to where the Java interpreter resides.)

 

8) You must put the actual name of the JAR file in the classpath, not just the path where it’s located.

 

9) If two libraries are imported via * and they include the same names ( for classes). Compile won't complain if you don't use the colliding name.

 

10) The Java access specifiers public, protected, and private are placed in front of each definition for each member in your class, whether it’s a field or a method. Each access specifier only controls the access for that particular definition. If you don’t provide an access specifier, it means “package access.”

 

11) package access (and sometimes “friendly”) means that 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.

 

12) When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone.

 

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

 

14) The private keyword means that no one can access that member except the class that contains that member, inside methods of that class. It’s not unlikely that a package might be created by several people collaborating together, so private allows you to freely change that member without concern that it will affect another class in the same package. Unless you must expose the underlying implementation (which is less likely than you might think), you should make all fields private.

 

15) Sometimes the creator of the base class would like to take a particular member and grant access to derived classes but not the world in general. That’s what protected does. protected also gives package access—that is, other classes in the same package may access protected elements.

 

16) Access control is often referred to as implementation hiding. Wrapping data and methods within classes in combination with implementation hiding is often called encapsulation.

 

17) Access control puts boundaries within a data type for two important reasons. The first is to establish what the client programmers can and can’t use and the second is to separate the interface from the implementation. For clarity, you might prefer a style of creating classes that puts the public members at the beginning, followed by the protected, package-access, and private members.

 

18) To control the access of a class, the specifier must appear before the keyword class. If you want a class to be available to a client programmer, you use the public keyword on the entire class definition. This controls whether the client programmer can even create an object of the class. If you just leave the public keyword off the class, it will has package access. (That class can be used only within that package.)

 

19) It is possible, though not typical, to have a compilation unit with no public class at all. In this case, you can name the file whatever you like.

 

20) Note that a class cannot be private (that would make it inaccessible to anyone but the class) or protected.(Actually, an inner class can be private or protected.)

 

21) If you don’t want anyone else to have access to that class (create instances of that class), you can make all the constructors private.

你可能感兴趣的:(package,classpath,Access Control,Encapsulation)