Http://blog.csdn.net/leeyu35/article/details/7755304
1. Static Method:
Member variables are dividedInstance variablesAndStatic variables. Instance variables belong to a specific instance and must exist only after class instantiation. Different objects have different instance variables. Static variables are public (equivalent to global variables) by all objects of the class and exist without instantiation.
Methods can also be dividedInstance methodAndStatic Method. The instance method must be called through an object after the class is instantiated, and the static method can be used before the class is instantiated. Unlike member variables:No matter which method, there is only one copy in the memory-the same method is shared no matter how many instances of the class are there.
Call an instance method:
Classa A = new classa (); // It must be instantiated.
A. instancemethod ();
Call static methods:
A. staticmethod (); // No need to be instantiated
2. Description and definition of static methods
Defining a static method is no different from defining an instance method in form. You only need to add a keyword static in the declared header. Its syntax is as follows:
[Access permission modifier] Static [Return Value Type] Method Name ([parameter list]) {
Statement Sequence
}
For example, the following is a static method:
Public static void stfun (){
System. Out. println ("this is a static method ");
}
3. Differences between static methods and instance methods
The differences between static methods and instance methods are mainly reflected in two aspects:
● ExternalWhen calling static methods, You can useClass Name. Method NameYou can also useObject Name. Method Name. The instance method is only followed by this method. That is to say,You can call static methods without creating objects..
● When a static method accesses a member of this class,Only allow access to static members(Static member variables and static methods), andAccess to instance member variables is not allowedAndInstance methodThe instance method does not have this restriction.
Instance code: static method access member variable example
Class accessmember {
Private Static int SA; // defines a static member variable.
Private int IA; // defines an instance member variable.
// A static method is defined below
Static void statmethod (){
Int I = 0; // correct. You can have your own local variables.
Sa = 10; // correct. Static variables can be used in static methods.
Otherstat (); // correct. You can call static methods.
IA = 20; // error. instance variables cannot be used.
Insmethod (); // error. The instance method cannot be called.
}
Static void otherstat (){
}
// Define an instance method below
Void insmethod (){
Int I = 0; // correct. You can have your own local variables.
Sa = 15; // correct. You can use static variables.
IA = 30; // correct. You can use instance variables.
Statmethod (); // correct. You can call static methods.
}
} // End of class accessmember
4. Static code block
In the class, you can declare a piece of code as static. Such a program block is called a static initialization segment. The static code block is generally in the following format:
Static {
Statement Sequence
}
● A static code block can only be defined in a class. It is independent of any method and cannot be defined in a method.
● The variables in the static code block are local variables and are only valid in the block.
● The static code block is automatically executed when the class is loaded, regardless of whether the loader is JVM or other classes.
● Multiple Static code blocks can be defined in a class. The execution sequence is determined by the defined sequence.
● A static code block can only be a static member of a category, but cannot be accessed by instance members.
Public class staticblock {
// Define a common main () method
Public static void main (string ARGs []) {
System. Out. println ("this is main method .");
}
// Define a static code block
Static {
System. Out. println ("this is static block .");
Int stvar = 0; // This is a local variable and is valid only in this block.
}
}
After the program is compiled, use the Java command to load the program and obtain the following output:
This is static block.
This is main method.
As shown in the preceding output,The static code block is executed even before the main method.. Tasks that can be completed in the main () method can be completed in static code blocks. However, there are still some differences between the two in execution. The main method is the entrance to the entire program startup, while the static code block is a process that exists in a class.
5. static member variables
Java allows classes as the types of static member variables, so static member variables are an object. For static member variables of the basic data type, they can be directly used outside the class without creating objects. However, if static members are objects, the problem is much more complicated. Because the class to which the object belongs may have both static members and instance members. The instance members must be used after the object is instantiated. The core issue is: whether the system will create instances for static class variables.
// ----------- File name supplytest. Java -----------------
Public class supplytest {
// Define a static Method for Testing
Public static void statshow (){
System. Out. println ("this is a static method ");
}
// Define an instance method for testing
Public void instshow (){
System. Out. println ("this is the instance method ");
}
} // End of supplytest. Java
// ----------- File name supplytest. Java -----------------
The following program defines a supplytest type variable. As a static member, it is instantiated without display.
// ----------- File name hasstatmember. Java -----------------
Public class hasstatmember {
Static supplytest stvar; // defines a static member.
Public static void main (string ARGs []) {
Stvar. statshow (); // call the static method
Stvar. instshow (); // call the instance method
}
}
// ----------- File name hasstatmember. Java -----------------
This program can be compiled, but the running result is as follows:
This is a static method
Exception in thread "Main" Java. Lang. nullpointerexception
At hasstatmember. Main (hasstatmember. Java: 5)
The running result shows that the static method is executed normally, but the instance method cannot be executed because no object instance is created. This means that although stvar is declared as static, the system will not automatically create an object for it, so the program must be changed to the following content to run properly:
// ----------- File name hasstatmember. Java -----------------
Public class hasstatmember {
Static supplytest stvar = new supplytest (); // defines a static member and instantiate it.
Public static void main (string ARGs []) {
Stvar. statshow (); // call the static method
Stvar. instshow (); // call the instance method
}
}
// ----------- File name hasstatmember. Java -----------------
The output result of the program is:
This is a static method
This is an instance method
From the output results, we can see that stvar instantiation is completed during definition, which means that stvar can be used outside the hasstatmember class as internally. The following program demonstrates the use of stvar.
// ----------- File name usestvar. Java -----------------
Public class usestvar {
Public static void main (string ARGs []) {
Hasstatmember. stvar. statshow (); // call the static method
Hasstatmember. stvar. instshow (); // call the instance method
}
}
// ----------- File name usestvar. Java -----------------
The output result of the program is as follows:
This is a static method
This is an instance method
Both static method and instance methodClass Name. static variable name. Method Name. Readers may feel familiar with this form. Indeed, the "system. Out. println" widely used above is in this form. Among them, system is a predefined class of the system, out is a static member of it, And println is an instance method of out.
6. initialization sequence in Java
When a Java class is loaded for the first time, it initializes static member variables or methods, but the method is not executed if it is not called,Static member variables are at the same level as static initialization blocks.,Non-static member variables and non-static initialization BlocksThe level is the same.
Initialization sequence: Initialize firstStatic code of the parent class---> InitializationStatic code of subclass-->
(If you do not create an instance when creating an instance, the subsequent initialization will not be performed .)Non-static code of the parent class(Variable definition, etc.) ---> InitializationParent class Constructor---> InitializationNon-static code of subclass(Variable definition, etc.) ---> InitializationSubclass Constructor
Classes are loaded by the Java class loader to create class instances only when they are created using the new call. When class instances are created based on the parent-child inheritance relationship, first, the initialization part is executed first, and then the constructor. Then, the initialization part of the subclass inherited from this class is executed. Finally, when the constructor class of the subclass is eliminated, the subclass part is first eliminated, eliminate the parent class