The examples in this article describe the use of static methods in Java. Share to everyone for your reference, specific as follows:
static denotes "global" or "static" meaning, used to decorate member variables and member methods, or to form static-code blocks, but the concept of global variables is not available in the Java language.
The member variables and member methods that are decorated by static are independent of any object of the class. That is, it does not rely on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machines can find them in the Run-time data area or the method area based on the class name. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.
Static member variables and member methods that are decorated with public are both global and global methods, and when you declare an object of its class, you do not generate a copy of the static variable, but all instances of the class share the same static variable.
A static variable can have a private adornment before it means that the variable can be used in a static code block of a class, or in other static member methods of a class (which can, of course, be used in a Non-static member method), but it is important that the class name is not referenced directly in other classes. In fact, you need to understand that private is the access permission limit, static means not instantiated can be used, so easy to understand more. The effect of the preceding static plus the other access rights keyword is also so.
The member variables and member methods of the static modifier are customarily called static variables and static methods, which can be accessed directly through the class name, and Access syntax is:
Class name. static method Name (parameter list ...)
Class name. Static variable Name
A code block decorated with static is a block of static code that executes when the Java Virtual Machine (JVM) loads the class (very useful).
Static variable
There are two types of class member variables that are statically modified: a static variable or a class variable, or a variable that is not modified by static, called an instance variable. The difference between the two is:
For static variables in memory only one copy (save memory), the JVM only for static allocation of memory, in the process of loading classes to complete the memory allocation of static variables, the use of the class name direct access (convenient), of course, can also be accessed through the object (but this is not recommended).
For instance variables, each time an instance is created, the instance variable is allocated memory once, and the instance variable can have more than one copy in memory, with no effect (flexibility).
static method
Static methods can be invoked directly through the class name, and any instance can be invoked, so static methods cannot use the This and Super keywords, and cannot directly access instance variables and instance methods of the owning class (that is, member variables and member methods without static). Only static member variables and member methods of the owning class can be accessed. Because instance members are associated with a particular object!
Because the static method is independent of any instance, the static method must be implemented, not abstract.
Static code block
A static code block is also called a code block, which is a static block of statements that is independent of class members in a class, can have multiple, can be positioned casually, it is not in any method body, and the JVM executes these static blocks of code when it loads classes, if there are multiple static code blocks, The JVM executes them sequentially in the order in which they appear in the class, and each block of code is executed only once. For example:
Package staticpackage;
public class Teststatic {
private static int a;//static variable
private int b;//instance variable
static {
TESTSTATIC.A = 3;
system.out.println (a);
Teststatic t = new teststatic ();
T.f ();
t.b = 1000;
System.out.println (t.b);
}
static {
TESTSTATIC.A = 4;
System.out.println (a);
}
public static void Main (string[] args) {
//TODO automatically generate method stub
}
static {
teststatic.a = 5;
System.out.println (a);
}
public void F () {
System.out.println (' method f () executing ... ');
}
/**
* 3
* method F () executing
... * 1000
* 4 *
5
*
*
Static blocks of code can be assigned to some static variables, and finally take a look at these examples, there is a static Main method, so that the JVM when running the main method can be called directly without creating an instance. Now we're going to see what it means to have public static void main (String args[) in instance Analysis Java. This article should be understood.
I hope this article will help you with Java programming.