static means "global" or "static", which is used to modify member variables and member methods, and can also form static static code blocks, but there is no concept of global variables in the Java language.
Member variables and member methods that are modified by static are independent of any object of the class. That is, it does not depend on class-specific instances and is shared by all instances of the class.
As long as this class is loaded, the Java virtual machine can find them in the method area of the runtime data area according to the class name. Therefore, a static object can be accessed before any of its objects is created without reference to any object.
The static member variables and member methods modified with public are essentially global variables and global methods. When declaring the object city of its class, it does not generate a copy of the static variable, but all instances of the class share the same static variable.
static variables can be modified with private, indicating that this variable can be used in the static code block of the class, or in other static member methods of the class (of course, it can also be used in non-static member methods - nonsense), but cannot be used in other classes It is important to directly refer to the class name in the class. In fact, you need to understand that private is a restricted access permissions, and static means that it can be used without instantiation, which will make it much easier to understand. The effect of preceding static with other access keywords is also similar.
Member variables and member methods modified by static are commonly called static variables and static methods. They can be accessed directly through the class name. The access syntax is:
Class name. Static method name (parameter list...)
Class name. Static variable name
A static code block is used to represent a static code block. When a Java virtual machine (JVM) loads a class, it will execute the code block (it is very useful, haha).
1. Static variables
Class member variables can be classified according to whether they are statically modified: one is a variable that is static or class variable; the other is a variable that is not statically modified, called an instance variable.
The difference between the two is:
For static variables only have one copy in memory (memory saving), the JVM only allocates memory once statically. The memory allocation of static variables is completed during the loading of the class. It can be accessed directly by class names (convenient). Of course, it can also be used through objects. Visit (but this is not recommended).
For instance variables, if an instance is not created, memory will be allocated to the instance variable once. The instance variables can have multiple copies in memory and will not affect each other (flexible).
Therefore, static variables are generally used when the following two functions are needed:
When sharing values between objects, when accessing variables
2. Static method
Static methods can be called directly through class names, and any instance can also be called.
Therefore, the keywords this and super cannot be used in static methods, and the instance variables and instance methods of the class belonging to cannot be directly accessed (that is, member variables and member member methods without static), and can only access the static member variables and member methods of the class belonging to.
Because instance members are associated with specific objects! This requires understanding and understanding the truth, not memory! ! !
Because the static method is independent of any instance, the static method must be implemented, not an abstract abstract.
For example, to facilitate the call of methods, all methods in the Math class in the Java API are static, and the static methods inside general classes are also convenient for other classes to call this method.
Static methods are a special class of methods inside a class. The corresponding method is declared as static only when needed. Methods inside a class are generally non-static.
3. Static code block
Static code blocks are also called static code blocks. They are static statement blocks that are independent of class members in a class. They can have multiple locations and can be placed at will. They are not in any method body. These static code blocks will be executed when the JVM loads the class. , If there are multiple static code blocks, the JVM will execute them in sequence in the order they appear in the class, and each code block will be executed only once. For example:
public class Test5 { private static int a; private int b; static{ Test5.a=3; System.out.println(a); Test5 t=new Test5(); tf(); tb=1000; System. out. println(tb); } static{ Test5.a=4; System.out.println(a); } public static void main(String[] args) { // TODO automatically generates method stub} static{ Test5.a=5 ; System.out.println(a); } public void f(){ System.out.println("hhahhah"); } }
Running results:
3
hhahhahah
1000
4
5
Using static code blocks, you can assign values to some static variables. Finally, you will take a look at these examples. They are all static main method, so that the JVM can directly call them when running the main method without creating an instance.
4. What does static and final mean together?
static final is used to modify member variables and member methods, which can be simply understood as "global constants"!
For variables, the representation cannot be modified once the value is given and can be accessed by the class name.
For methods, the representation is not overridden and can be accessed directly through the class name.
Sometimes you want to define a class member so that it uses completely independent of any object of that class. Typically, a class member must be accessed through an object of its class, but a member can be created that can be used by it itself without having to refer to a specific instance. This member can be created by preceding the member's declaration. If a member is declared static, it can be accessed before any object of its class is created without having to refer to any object. You can declare both methods and variables as static. The most common example of a static member is main() . Because main() must be called when the program starts executing, it is declared static.
A variable declared as static is essentially a global variable. When an object is declared, no copy of the static variable is generated, but all instance variables of the class share the same static variable. Methods declared as static have the following restrictions:
They can only call other static methods.
They only have access to static data.
They cannot refer to this or super in any way (the keyword super is related to inheritance, described in the next chapter).
If you need to initialize your static variable by calculation, you can declare a static block, which is executed only once when the class is loaded. The following example shows the class with a static method, some static variables, and a static initialization block:
// Demonstrate static variables, methods, and blocks. class UseStatic { static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out. println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); } }
Once the UseStatic class is loaded, all static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a*4 or 12. Then call main(), main() calls meth(), and pass the value 42 to x. The 3 println ( ) statements refer to two static variables a and b, and the local variable x.
Note: It is illegal to refer to any instance variable in a static method.
Here is the output of the program:
Static block initialized.
x = 42
a = 3
b = 12
Outside the classes that define them, static methods and variables can be used independently of any object. In this way, you just need to add a dot operator after the class name. For example, if you want to call a static method from outside the class, you can use the following general format:
classname.method( )
Here, classname is the name of the class, where the static method is defined. As you can see, this format is similar to the format of calling non-static methods through object reference variables. A static variable can be accessed in the same format - the class name dot operator. This is a control version of how Java implements global functions and global variables.
Here is an example. In main(), the static method callme() and the static variable b are accessed outside their class.
class StaticDemo { static int a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } } class StaticByName { public stat ic void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); } }
Here is the output of the program:
a = 42
b = 99
The static member cannot be accessed by the instances created by the class they are located in.
If a member without static modification is an object member, it belongs to each object.
The member with static modification is a class member, which can be called directly by a class and shared by all objects.
Through this article, have you any understanding of the role of static in Java? In the future, the editor will also organize relevant articles on static for you, hoping that it will be helpful to your learning.