JAVA basis of the access control rights

Package: the library unit

1. When writing a Java source code file, which is commonly referred coding unit (sometimes also referred to as translation unit).
2. Each compilation unit must have a suffix .java, but you can have a public class in the compilation unit, the class name and the name of the file must be the same.
3. Each compilation unit can only have one public class, or the compiler will not accept.
4. If in the compilation unit there are additional classes, then in the world outside of the package are not visible these classes because they are not public class.
  • Code Organization
xx.java compiled xx.class
package statement must be the first sentence of the program code file other than comments

package:

 
[java]  view plain  copy
 
  1. package com.util.tools;  
  2.   
  3. public class Tools {  
  4.     public static void print(Object object) {  
  5.         System.out.println("" + object);  
  6.     }  
  7. }  

import:

 
[java]  view plain copy  
 
  1. package com.test;  
  2.   
  3. package com.test;  
  4.   
  5. import static com.util.tools.Tools.*; //注意  
  6. import java.util. *;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) {  
  10.         print("Hello Java.");  
  11.         print(Calendar.getInstance().getTime().toString());  
  12.     }  
  13. }  
  • Create a unique package name
By convention, the first part of the package name is the reverse order of the creator of the class of Internet domain names

Running process java interpreter

1. Find the environment variable CLASSPATH, CLASSPATH contains one or more directories used to find the root directory of the .class file
2. Start from the root directory, the name of the interpreter acquires the package and replace each backslash periods to generate a path from the root name CLASSPATH
3. The resulting path will be connected to the various items in the CLASSPATH, the interpreter will find associated with the class name you want to create .class files in those directories

Note: CLASSPATH = .; D: \ JAVA; C \ actual name of flavors \ grape.jar // JAR file which needs to write clearly

Java access modifiers

public - protected - packet access (no keyword) - private
Before each member into a defined class, whether it is a field or method
Access control permissions often become: concrete realization of hidden
  • A way to get access to the members of the
1. enable members to become public
2. in the same package, without access qualifier
3. Using inheritance
4. The device provides access to and variation (get / set method)
  • public: Interface Access
Any class can access
  • protected: inheritance access
package provides protected access rights protected access to other classes in the same package
  • Package access
Default permissions, no keywords
The current class has access to all other members of that package of rights for all classes outside of the package, this member is private
Default Package: in the same directory, are not defined package, the package is having access
  • private
In addition to containing the class members outside any other class can access this member
 
[java]  view plain  copy
 
  1. package com.test;  
  2.   
  3. public class Num1 {  
  4.     protected static int i = 0;  
  5.     static int j = 0;  
  6.     private static int k = 0;  
  7. }  
  8.   
  9. class Num2 {  
  10.     int i = Num1.i;  
  11.     int j = Num1.j;  
  12.     // int k = Num1.k; // can not access private, the compiler error  
  13. }  
  14.   
  15. class Num3 extends Num1 {  
  16.     int a = i;  
  17.     int b = j;  
  18.     // int c = k; // subclass can not access a private field, given compiled  
  19. }  

Class Access

Public class and may access the package, but not (with the exception inner class) private and protected in.
If you do not want anyone else to have access to the classes, the constructor can be designated as private, so as to prevent the creation of such objects, but you can create objects within that class. [Singleton]
 
[java]  view plain  copy
 
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Camera mCamera = Camera.newCamera();  
  4.     }  
  5. }  
  6.   
  7. class Camera {  
  8.     private static Camera mCamera = new Camera();  
  9.     private Camera() {}  
  10.   
  11.     public static Camera newCamera() {  
  12.         return mCamera;  
  13.     }  
  14. }  

to sum up

  • access permission
  Inner classes This package Subclass External packaging
public
protected ×
Package access × ×
private × × ×
  • Access Use
1. Class can only use public, protected
2. The method, member variables, local variables can be used more than four permission

access permission

  • Members access
  1. Default: not declare anything, the package access, under the same package can access, not access the external package.
  2. public: you can access.
  3. protected: inheritance access to its subclasses can access; also access package, the same package can access.
  4. private: outside the class is not accessible.
  • Class Access
  1. public:
  2. Package access: At this point the best domain declare the class as private makes sense.

Note: Class access rights can not be private, and if you do not want any other type of access, such constructor can be defined as private, can not let others define the objects of the class!

      protected does not work!

  • If a class constructor is private

    Can define a public static class name of the method, the method returns an object in the call to the constructor of this class references (return new leiming ()) in this class. Then outside the class, call this method to create an object reference class.



Guess you like

Origin www.cnblogs.com/jiangzhaowei/p/8126364.html