One, Java class package
After writing the good one source program, the first thing to do is to execute the javac command to compile the source file and generate a bytecode file (class file) with the same name as the source file with the suffix ". Class", which is in the same directory as the source file by default. This type of file is created by the user, and in the Java programming environment, Java itself provides a large number of class files, which can be directly introduced in the programming, which greatly facilitates the program writing.
Package definition: Is the basis of Java programming, the package is contained in the standard class of architecture and the user's own program, the package is a collection of uniquely named classes. In Java, packages are divided into user packages and system packages.
A) User package: The package that is created by the user in the project.
At the command prompt, when you compile a program using JAVAC, the class file is generated, and the file defaults to the source file in the same directory, and if a project is large, in order to facilitate organization and management, it is common to organize different classes to manage them, which requires the user to customize the package
Claim Creation Package
Package < pack name 1>. [< package name 2>. [< package name 3>, ..... ]]
Here, the package is the folder name under the root of the source program, but slightly different from the window directory structure notation, the slash in the window directory "\" with the solid point "." To replace.
For example, Helloworld.java source file in the C:\javaTest directory, for its source code declaration of a package, the source code is as follows:
Package Lib.hello
public class HelloWorld
{
Getting started with the main function of the program
Public staticvoid Main (String args[])
{
Output Print character statement
System.out.println ("helloworld! ");
}
}
Steps to execute the program:
1, enter C:\javaTest at the command prompt , compile the source program: Javac Helloworld.java, Generate Helloworld.class class file.
2. Create a new Lib folder in the C:\javaTest directory, create a new Hello folder in the Lib folder, and cut the HelloWorld class file into the Hello folder.
3. Running source program: Java HelloWorld, Output "helloworld! Characters
Rules for creating packages:
1. The package statement must be placed at the front of the source file, and there should be no other statements before it.
2, each source file has a maximum of one package statement, because a class file can not belong to two packages,
3. A package can contain multiple packages that contain relationships using a solid point "." To express. such as the statement "package Lib.hello", the Lib package contains a Hello packet, where the LIB package in the program (project) root directory, to use the class file in Hello, must start from the program (project) root directory of the package.
The introduction of the package mechanism is reflected in the following aspects:
L be able to implement control of access rights.
L use packages to distinguish between classes with the same name. Classes with the same name are not allowed in the same package, and classes with the same name can exist in different packages.
The package can be used for different class file partitioning and organization management.
II) System Package
System Package refers to the Java programming platform itself has been defined by a number of categories,Java provides users with more than 130 pre-defined packages, reference after the program can be directly used in the class, the following is a brief introduction:
1, Java.lang Package: Java Core class Library, contains the necessary system classes to run Java programs, such as basic data types, basic mathematical functions, string processing, threading, exception handling classes, the system default load this package
2, java.io package: Java language standard input/output class library, such as basic input/output stream, file input/output, filter input/output stream, etc.
3. Java.util Package: Contains the date class such as processing time, the vector class that handles the array, and the stack and Hashtable classes
4, java.awt package: Build a graphical user interface (GUI) class library, low-level drawing operations graphics class, graphical interface components and layout management such as the CheckBox class, Container class, Layoutmanger interface, and interface user interaction control and event response, such as the event class
5. Java.awt.image Package: Java Tool class library for processing and manipulating images from the web
6. Java.wat.peer Package: Rarely used directly in the program, making the same Java program run on different hardware and software platforms
7. Java.applet Package: Contains all classes that implement Javaapplet.
8. java.net Package: The class library that realizes network function has socket class, ServerSocket class
9. Java.corba bag and Java.corba.orb bag
10. Java.lang.reflect Package: Provides tools for reflecting objects
11, Java.util.zip Package: To achieve file compression function
12. Java.awt.datatransfer Package: Tool class for handling data transfer, including clipboard, string transmitter, etc.
13. Java.awt.event Bag
14. java.sql Package: Implementing the JDBC Class library
15. Java.rmi provides remote connection and loading support.
16, Java.security provides security related support.
III) Reference Package
To use a user-created package, or to use a class package that comes with the system, here's how:
Method One: Use the package directly, that is, precede the class name to be referenced with the package name as the modifier. Typically used in cases where the number of classes or interfaces in other packages is referenced less frequently.
For example: Lib.hello B = new Lib.hello ();
Method Two: Use the Package reference statement import.
In a Java program, you can define multiple import statements. If there is a package statement, the import statement is immediately followed, otherwise the import statement should be in the first statement of the program. There are two more ways to use import Introduction:
(1) Import < package name;. < class name >
Imports a specified public class or interface.
(2) import〈 package name;. *
Import all classes or interfaces that are required for the current class in the package, and note that classes in other folders in the package cannot be referenced at this time. This method is generally not recommended because it often causes the program to load many unwanted classes, increasing the load on the system, and increasing the probability of class name collisions. It is therefore generally recommended to use the first format.
For example:
Import Lib.hello;
Import Lib.hello. *;
Import java.io.*;
Import Jsit.util.Date;
Second, Java access modifiers, modifiers
By referencing a class package, you can implement access control on the class. In addition, Java's access modifiers are able to precisely control the access control of classes, classes, variables, and methods.
Class:
access modifier modifiers class name extends parent class name Implement Interface name 1[, Interface Name 2] ...
(The location of the access modifier and the modifier can be interchanged)
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed by all classes (using) |
The public class must be defined in a file with the same name as the class name |
Package |
can be accessed by a class in the same package (using) |
The default access rights, which can be omitted from this keyword, can be defined in the same file as the public class |
Private |
modifying inner classes |
|
Modifier |
Name |
Description |
Note |
Final |
Classes that use this modifier cannot be inherited |
|
Abstract |
If you want to use the abstract class, you must first build a new class that inherits the abstract class, and the new class implements the abstraction method in the abstract class. |
class must be defined as abstract, as long as there is an abstract method, but the abstract class is not necessarily protected by the abstract method |
Variable
There are no global variables in Java, only method variables, instance variables (non-static variables in a class), class variables (static variables in a class).
The variables in the L method cannot have access modifiers. So the following access modifier table is only for variables defined in the class.
L When declaring an instance variable, if no initial value is assigned, it will be initialized to null (reference type) or 0, False (original type).
The more complex instance variables can be initialized by an instance variable initializer, which is a block of statements with {}, which runs after the constructor of the class is invoked, before the constructor of the parent class constructor.
The L class variable (static variable) can also be initialized by a class-variable initializer, which is a block of statements contained in static{} and may only be initialized once.
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed by any class |
|
Protected |
can be accessed by all classes in the same package can be accessed by all sub-classes |
Subclasses are not accessible in the same package |
Private |
can only be accessed by methods of the current class |
|
Default No access modifier |
can be accessed by all classes in the same package |
If the subclass is not in the same package, you cannot access the |
Mainly because the amount of change can come from other variables, use it for performance problems
modifier |
name |
Description |
remarks |
static |
Static variable (also known as class variable, other called instance variable) |
Can be shared by all instances of the class. Does not need to create an instance of the class to access the static variable |
Final |
Constant, value can only be assigned once, Cannot change |
Take care not to use const, although it has the same meaning as the Const keyword in C, C + + Can be used with static to avoid maintaining a copy of each instance of the class |
transient |
Tell the compiler that this variable does not need to persist when serializing a class object | TD valign= "Top" >
volatile |
Indicates that there may be multiple threads modifying this variable, requiring the compiler to optimize to ensure that modifications to this variable are handled correctly |
|
Method
access modifier modifier return type Method Name ( parameter list ) throws list of violations
The constructor method of the L class can not have modifiers, return types, and throws clauses
When the constructor method of the L class is called, it first calls the constructor method of the parent class, and then runs the initializer for the instance variable and the static variable before the constructor itself is run.
L If the constructor method does not display a constructor that calls a parent class, the compiler automatically adds a default super () to it, and if the parent does not have a default parameterless constructor, the compiler will error. Super must be the first clause of a constructor method.
L NOTE the use of the private constructor method.
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed from all classes |
|
Protected |
can be accessed by all classes in the same package can be accessed by all sub-classes |
Subclasses are not accessible in the same package |
Private |
can only be accessed by methods of the current class |
|
Default No access modifier |
can be accessed by all classes in the same package |
If the subclass is not in the same package, you cannot access the |
Modifier |
Name |
Description |
Note |
Static |
Static methods (also known as class methods, others called instance methods) |
Provide services that do not depend on class instances You do not need to create an instance of a class to access a static method |
Final |
Prevent any subclasses from overloading the method |
Be careful not to use const, although it has the same meaning as the Const keyword in C, C + + Can be used with static to avoid maintaining a copy of each instance of the class |
Abstract |
Abstract methods, methods that have been declared in a class and not implemented |
You cannot declare a static method, a final method, or a constructor method of a class as abstract |
Native |
The method defined with this modifier is not implemented in the class, and in most cases the implementation of the method is written in C and C + +. |
See Sun's Java Native Interface (JNI), which provides the runtime to load an implementation of a native method and associate it with a Java class function |
Synchronized |
Multi-threaded support |
When a method is called, no other thread can call the method, and the other synchronized method cannot call the method until the method returns |
Interface
access Modifiers Interface Interface Name extends Interface list
The L interface is not able to define any implementations of its declared methods
Variables in the L interface always need to be defined as "public static final interface name", but can not contain these modifiers, the compiler defaults to this, the display contains modifiers mainly for the program clear
Access modifiers |
Name |
Description |
Note |
Public |
All |
|
No access modifier (default) |
In the same package |
|
Access control for Java (package, access modifier, modifier)