`
leonzhx
  • 浏览: 793378 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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.

分享到:
评论

相关推荐

    Addison.Wesley.Test-Driven.iOS.Development

    Chapter 6. The Data Model Chapter 7. Application Logic Chapter 8. Networking Code Chapter 9. View Controllers Chapter 10. Putting It All Together Chapter 11. Designing for Test-Driven Development ...

    《Java实用系统开发指南》CD光盘

    ..\........\chapter6.rar ..\........\readme.txt ..\chapter7 ..\........\7-1 ..\........\...\EJBDefinition.java ..\........\7-2 ..\........\...\ServiceClientFactory.java ..\........\7-3 ..\......

    The.Cucumber.for.Java.Book.Behaviour-Driven.Development

    Chapter 6. Keeping Your Cucumbers Sweet Part II A Worked Example Chapter 7. Step Definitions: On the Inside Chapter 8. Support Code Chapter 9. Message Queues and Asynchronous Components Chapter 10. ...

    Digital Signal Processing Using Matlab

    Chapter 6. Linear and Invariant Discrete-Time Systems Chapter 7. Infinite Impulse Response Filters Chapter 8. Finite Impulse Response Filters Chapter 9. Detection and Estimation Chapter 10. Power ...

    PHP & MySQL范例精解

    Chapter 6 - Contains the source code for the AJAX File Manager project. Chapter 7 - Contains the source code for the Online Photo Album Chapter 8 - Contains the source code for the Shopping Cart ...

    Pro.Java.8.Programming.3rd.Edition.1484206428

    Chapter 6. Using Layout Managers Chapter 7. Using Swing’s JTable Chapter 8. Using Swing’s JTree Chapter 9. Adding Cut-and-Paste Functionality Chapter 10. Adding Drag-and-Drop Functionality Chapter ...

    Pro.Java.8.Programming.3rd.Edition.1484206428.epub

    Chapter 6. Using Layout Managers Chapter 7. Using Swing’s JTable Chapter 8. Using Swing’s JTree Chapter 9. Adding Cut-and-Paste Functionality Chapter 10. Adding Drag-and-Drop Functionality Chapter ...

    《WindowsPE权威指南》附书源代码.rar

    -chapter6 堆栈与重定位表 -HelloWorld.exe -chapter7 资源表 -HelloWorld.exe -chapter8 延迟导入表 -HelloWorld.exe -chapter9 线程局部存储 -HelloWorld.exe -chapter10 加载配置信息 -HelloWorld.exe...

    Learning.Node.Moving.to.the.Server-Side.2nd.Edition.14919

    Chapter 6. Node and the Local System Chapter 7. Networking, Sockets, and Security Chapter 8. Child Processes Chapter 9. Node and ES6 Chapter 10. Full-stack Node Development Chapter 11. Node in ...

    PC-lint 9.0 -- Gimpel.Software.PC-lint.9.0a

    mainly from the compiler options files (co-...lnt). For example options files co-msc70.lnt, co-msc71.lnt, co-msc80.lnt and co-msc90.lnt support versions 7.0, 7.1, 8.0 and 9.0 respectively. -----...

Global site tag (gtag.js) - Google Analytics