`
leonzhx
  • 浏览: 770682 次
  • 性别: 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 ...

    Advanced.Java.Programming.0199455503

    Chapter 6. Generic Programming Chapter 7. Reflection Chapter 8. Java Native Interface Chapter 9. AWT and Swing Chapter 10. Java and XML Chapter 11. Input/Output PART II: NETWORK PROGRAMMING Chapter ...

    Java Programming for Kids

    Chapter 6. Access Levels, Collections, Generics, and Multi-Threading Chapter 7. Introduction to GUI with JavaFX Chapter 8. Programming User Interaction in JavaFX Chapter 9. Dealing With Run-time ...

    iron-clad.java.building.secure.web.applications

    Chapter 6. Protecting Sensitive Data Chapter 7. SQL Injection and Other Injection Attacks Chapter 8. Safe File Upload and File I/O Chapter 9. Logging, Error Handling, and Intrusion Detection Chapter ...

    Process.Control.A.Practical.Approach.2nd.Edition.1119

    Chapter 6. Feedforward Control Chapter 7. Deadtime Compensation Chapter 8. Multivariable Control Chapter 9. Inferentials and Analysers Chapter 10. Combustion Control Chapter 11. Compressor Control ...

    React Native in Action

    Chapter 1. Getting Started With React Native Chapter 2. Understanding React Chapter 3. Building Your First React Native App PART 2 Developing applications in React Native Chapter 1. Introduction To ...

    SPSS.Statistics.23.Step.by.Step

    Chapter 6. Graphs & Charts Chapter 7. Frequencies Chapter 8. Descriptive Statistics Chapter 9. Crosstabulation & χ2 Analyses Chapter 10. Means Procedure Chapter 11. Bivariate Correlation Chapter 12. ...

    Borland C++ Builder 6 Quick Start 帮助文档 (英文,PDF)

    Borland C++ Builder 6 Quick Start 帮助文档 (英文,PDF) Chapter 1 Introduction 1-1 What is C++Builder? ...1-1 Registering C++Builder ...1-2 Finding information ...1-3 Online Help ...1-3 F1 Help ...1-4 ...

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

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

    06.Chapter6-Capacitors and Inductors.pptx

    06.Chapter6-Capacitors and Inductors.pptx

    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. ...

    Real-Time.Software.Design.for.Embedded.Systems

    Chapter 6. Use Case Modeling for Real-Time Embedded Systems Chapter 7. State Machines for Real-Time Embedded Systems Chapter 8. Object and Class Structuring for Real-Time Embedded Software Chapter 9. ...

    Java与模式.part07-09.rar

    Java与模式.part07-09.rar

    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 ...

    MyEclipse.6.Java.(chapter1-10).rar

    MyEclipse.6.Java.(chapter1-10)

    Creating.a.Data-Driven.Organization.1491916915

    Chapter 6. Metric Design Chapter 7. Storytelling with Data Chapter 8. A/B Testing Chapter 9. Decision Making Chapter 10. Data-Driven Culture Chapter 11. The Data-Driven C-Suite Chapter 12. Privacy, ...

    Packt.Go.Design.Patterns.2017

    Chapter 6. Behavioral Patterns - Template, Memento, and Interpreter Design Patterns Chapter 7. Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns Chapter 8. Introduction to ...

    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 ...

    Electronic Filter Design Handbook

    Chapter 6. Band-Reject Filters Chapter 7. Networks for the Time Domain Chapter 8. Refinements in LC Filter Design and the Use of Resistive Networks Chapter 9. Design and Selection of Inductors for...

Global site tag (gtag.js) - Google Analytics