Java Package concept

Source: Internet
Author: User
The package definition statement must be placed in the first line of the Java source file, that is, there cannot be any other statements except blank and comments before it.

The package also corresponds to the directory hierarchy of the operating system. Therefore, the folder hierarchy must be defined when the corresponding package is defined. For example
The following is an example of test. Java:

Package mysite;

Class Test

{

Public
Static void main (string [] ARGs)

{

System. Out. println ("Package
Test successed! ");

}

}

Assume that test. java in E:/javalession/lession3, DOS enters this directory and runs javac test. java, a test is generated. class to compile the file. If the operation continues, run the compilation file: Java
Test. copy the class to the mysite folder and execute Java mysite in E:/javalession/lession3. test Runs successfully.

However, if there are many classes, it is very difficult to manually create the corresponding folder. We can use the-D command of javac to directly generate the corresponding folder hierarchy when compiling the source file:

Run javac-D in E:/javalession/lession3. test. java. After compilation, Java automatically generates a mysite folder and the class file test. class is under this mysite directory.

Note: The dot after-D indicates that the corresponding file directory is generated under the current directory.

Of course, we can also place the compiled compilation files to other directories, for example, to the D: disk root directory. You can execute javac-D in E:/javalession/lession3: /test. java
In this case, the mysite directory structure is generated on disk D. If you want to execute the test. Java file in drive D, you need to put d in the classpath path according to the root directory. Step:

CMD command: Set classpass = % classpass %; D. The content in % indicates the previously loaded path.

You can run set classpass to view the loaded path.

Note: Because the full name of the class with the package name is the package name + the class name, it is not feasible to enter the mysite directory and execute Java test. Must be refunded
To the current directory of the test. Java file, and then execute

Java mysite. test is successful.

 

ImportStatement

If there is a package, you need to import it.

Introduce the class in the package: Import
Java. Io. file;

Introduce the entire package:
Import java. Io .*;

Classes can be referenced in the same package without the need for import statements.

If you do not import the classes in other packages you want to use through import, you must use the full name of the class to access the class. Is it convenient to use import ???

 

Class description

Class access description:

Public if you want the classes in other packages to access you
For the current class, public must be added to the class.

Of course, when you access the classes in other packages, don't forget to import them using import.

By default
The package is accessed.

Other modifiers of the class

Final: This keyword indicates that the current class cannot be inherited.
Cheng, she is a final class. If you do not want the class to be inherited and modified,

Use final. For example, string is a final class.

Abstract
Abstract class

 

Method description
Character:

Method access instructions:

(1) Any class in different public packages can access each other.

(2) protected if the parent class of a subclass is a class in different packages, but because the parent class method uses protected

Off
Then the sub-classes in different packages can access this method of the parent class.

(3) default can only be accessed in the same package. For example, if the parent class of a subclass is a class in different packages

Sub-classes cannot access the default side of the parent class.
Method. Because subclass and parent class belong to different packages.

(4) Private can only be accessed in the same class, that is, the subclass inherits the parent class, and the private methods in the parent class are not

Yes
It is inherited to the subclass, so the subclass cannot access the private method of the parent class.

Other modifiers of the method:

(1) Static this method belongs to the class itself, rather than an instance of the class.

(2) In order to ensure that the class remains unchanged during the inheritance process, a good way for final not to allow the subclass to override the parent class is
Enable

Use the final keyword.

All the private and static methods in the class are actually the final method.

(3) Abstract METHODS without a method body are abstract methods. The specific manifestation is no {} and the content is only;

Note: The abstract method must end with a semicolon! If
If there are abstract methods, the class must be declared as an abstract class, and the abstract class cannot instantiate a class object. In addition, if a subclass inherits an abstract parent class
The subclass must also be declared as an abstract class unless the subclass implements all abstract methods.

The reason why an abstract class cannot generate any class instance is to prevent classes with implementation methods from generating any objects.

(4) Native methods that can be used but cannot be written in Java

To understand this method, you must first understand JNI technology (Java Native Interface)
It allows JVM to run
Line of Java code
Ability to work with other programming languages (C, C ++, etc)
Write Applications and libraries for interoperability. In short, JNI is the intermediary between Java and local applications. The specific process must be used in the javah.exe compiler.

(5) This modifier will be introduced when synchronized talks about multithreading.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.