Java learning diary 8-package, environment variables and access control, java8-
Java packages, environment variables, and Access Control
1. java package
Java uses packages to organize code, so that the code structure of large projects is clear, and packages are the division of namespaces, that is, classes with the same name can be included in different packages, you only need to add the package name before the class name to distinguish them.
Package xxx must be located in the first line except comments in the java file to indicate which package the class in the current file belongs to. If there is no Package statement, the classes in this file belong to the default package.
Import xxx is used to Import classes that do not belong to the current package in the current java file, so that they can be used in the current file.
II. Environment Variables in java
1. path
Environment variables are actually a group of variables (nonsense). They are used to provide parameters for systems and applications. For example, path indicates the storage location of some essential programs in the system and applications. For example, if you want to run the ipconfig command, Where can the system find it, locate the storage location one by one until it is found.
In java, when we want to run a java program and enter java xxx in the console, where can we find the java application (exe file? The system must use the path to copy the directory containing the java application to the path environment variable, if you run java xxx again, "'java' is neither an internal or external command, nor a program or batch processing file that can be run." .
2. classpath
After talking about the path, let's talk about classpath, which solves the problem of the java command and the javac command. Here it involves the package mechanism in java.
Java programs are composed of classes that can be called from each other. Some classes have been defined in the java language. Where can I find them when the program uses these classes, this requires the classpath environment variable to give you some advice.
Summary: classpath specifies the Class search path. If the class is not packaged, you need to add its directory to classpath. If the class is packaged as a JAR file, the actual name of the JAR file must be clearly written.
Classpath is generally set to ".; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar ;"
Where. indicates the current directory, and % JAVA_HOME % indicates the path of java jdk. Dt. jar mainly contains various control classes in the swing package. Tools. jar is a variety of tool classes.
When the compiler encounters an import statement, it starts to search in the directory contained in CLASSPATH.
(Reference: http://www.linuxidc.com/Linux/2012-01/52713.htm)
3. JAVA_HOME
Specifies the jdk path
Iii. access permission Control
1. Access Control for Class Members
Access Control letter: public/protected/package access permission (default)/private
|
Class itself |
Subclass in the same package |
Subclass of different packages |
Non-subclass in the same package |
Non-subclass of different packages |
In short |
Public |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Private |
Yes |
No |
No |
No |
No |
Visible only |
Default |
Yes |
Yes |
No |
Yes |
No |
Only visible to the same package |
Protected |
Yes |
Yes |
Yes |
Yes |
No |
Subclass or visible in the same package |
2. Class access control permissions
Class has only two types of access control permissions: default (package access control permissions) and public