1. Static method:
Member variables are divided into instance variables and static variables . Where an instance variable belongs to a specific instance, it must be instantiated before the class is actually present, and different objects have different instance variables. Static variables are public (equivalent to global variables) by all objects of the class and do not need to be instantiated to exist.
Methods can also be divided into example methods and static methods . Where the instance method must be called by the object after the class is instantiated, and the static method can be used before the class is instantiated. Unlike member variables: Either way, there is only one copy in memory-no matter how many instances of the class, the same method is shared.
Invocation of the instance method:
ClassA a = new ClassA (); Must be instantiated
A.instancemethod ();
Invocation of a static method:
A.staticmethod (); No need to instantiate
2. Declaration and definition of static methods
To define a static method and define an instance method, there is no difference in formality, except that the head of the declaration needs to be added with a keyword static. Its general grammatical form is as follows:
[Access modifier] static [return value type] Method name ([parameter list]) {
Statement sequence
}
For example, here is a static method:
public static void Stfun () {
System.out.println ("This is a static method");
}
3. The difference between static and instance methods
The difference between static method and instance method is mainly embodied in two aspects:
When you call a static method externally, you can use the " class name. Method Name " Way, or you can use the " object name. Method Name " way. The instance method is only in the following way. That is, calling a static method eliminates the need to create an object .
When a static method accesses members of this class, it only allows access to static members (that is, static member variables and static methods), not to instance member variables and instance methods , and the instance method does not .
Instance code: Static method Access member variable example
Class accessmember{
private static int sa; Define a static member variable
private int ia; Defining an instance member variable
The following defines a static method
static void Statmethod () {
int i = 0; Correct, can have their own local variables
SA = 10; Correct, static methods can use static variables
Otherstat (); Correct, you can call a static method
ia = 20; Error, cannot use instance variable
Insmethod (); Error, cannot invoke instance method
}
static void Otherstat () {
}
The following defines an instance method
void Insmethod () {
int i = 0; Correct, can have their own local variables
SA = 15; Correct, you can use static variables
IA = 30; Correct, you can use instance variables
Statmethod (); Correct, you can call a static method
}
}//end of Class Accessmember
4. Static code block
In a class, you can declare a piece of code as static, such that a block is called a static initialization segment. The general form of a static code block is as follows:
static {
Statement sequence
}
A static code block can only be defined within a class, it is independent of any method, and cannot be defined inside a method.
The variables inside the static code block are local variables and are valid only within this block.
Static blocks of code are executed automatically when the class is loaded, regardless of whether the loader is a JVM or another class.
A class allows you to define multiple static blocks of code, and the order in which they are executed is performed in the order defined.
Static code blocks can only access static members of a class, not access 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 block of code
static{
System.out.println ("This is static block.");
int stvar = 0; This is a local variable that is valid only within this block.
}
}
After compiling, the program is loaded with Java commands, and the following output is obtained:
This is a static block.
This is main method.
As you can see from the output above, a static block of code is executed even before the main method . Tasks that you can accomplish in the main () method can be done in a static block of code. However, there are still some differences in execution, the main method is the entry for the entire program to start, and the static code block is a procedure that exists in a class.
5. Static member variables
Java allows a class to be a type of static member variable, so a static member variable is an object. If it is a static member variable of the base data type, it can be used directly outside the class without creating an object. But if a static member is an object, the problem is much more complicated. Because the object belongs to a class, there can be either a static member or an instance member. Instance members must be instantiated before they can be used, at the core of the question is whether the system creates an instance of a static class variable.
-----------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 purposes
public void Instshow () {
System.out.println ("This is an example method");
}
}//end of Supplytest.java
-----------File name Supplytest.java-----------------
In the following program, a variable of type supplytest is defined as a static member and is not instantiated with a display.
-----------File name Hasstatmember.java-----------------
public class hasstatmember{
Static Supplytest Stvar; Define a static member
public static void Main (String args[]) {
Stvar.statshow (); Calling a static method
Stvar.instshow (); Invoking instance methods
}
}
-----------File name Hasstatmember.java-----------------
This program can be compiled through, but it runs the following results:
This is a static method
Exception in thread "main" java.lang.NullPointerException
At Hasstatmember.main (hasstatmember.java:5)
As can be seen from the running result, the static method is executed normally, but the instance method cannot be executed because the object instance is not created. This means that although the Stvar is declared as a static type, the system will not automatically create objects for it, so the program must be changed to work as follows:
-----------File name Hasstatmember.java-----------------
public class hasstatmember{
static Supplytest Stvar = new Supplytest (); Define a static member and instantiate it
public static void Main (String args[]) {
Stvar.statshow (); Calling a static method
Stvar.instshow (); Invoking instance methods
}
}
-----------File name Hasstatmember.java-----------------
The output of the program is:
This is a static method
This is an example method
As can be seen from the output, the instantiation of the Stvar is done at the time of definition, which means that the outside of the Hasstatmember class can be used as if it were 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 (); Calling a static method
HasStatMember.stVar.instShow (); Invoking instance methods
}
}
-----------File name Usestvar.java-----------------
The output of the program is as follows:
This is a static method
This is an example method
Both static and instance methods are used in the form of " class name. Static variable name. Method Name ". Readers may find this form somewhat familiar. Indeed, this is the form of "System.out.println", which is used extensively in the preceding. Where system is a predefined class of systems, out is a static member of it, and println is an instance method of out.
The order of initialization in 6.Java
The first time a Java class is loaded, a static member variable or method is initialized once, but the method is not invoked, and the static member variable is the same level as the static initialization block , and the non-static member variable and the non-static initialization block level are the same.
Initialization order: Static code that initializes the parent class ---> Initializes the subclass's static code--
(When creating an instance, if you do not create an instance, then do not execute) initializes the non-static code of the parent class (variable definition, etc.)---> initializes the parent class constructor ---> initializes the subclass non-static code (variable definition, etc.)---> Initialize subclass Constructors
Class is only loaded by the Java class loader when it is created with the new call, when the class instance is first initialized by a parent-child inheritance relationship, the first initialization of the block part executes first, then the constructor, and then the initialization block of the subclass inheriting from this class executes, Finally, when the subclass is constructed, the class is eliminated, the subclass part is eliminated, and the parent class is eliminated.
Java static methods, static variables, initialization order