Static means "global" or "static" and is used to modify member variables and member methods. It can also form a static code block, but there is no concept of global variables in the Java language.
Member variables and member methods modified by static are independent of any objects of the class. That is, it does not depend on a specific instance of the class and is shared by all instances of the class.
As long as this class is loaded, the Java virtual machine can find them by default in the method area of the runtime data 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 modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of the static variable is not generated, but all instances of the class share the same static variable.
A static variable can be preceded by a private modification, which means 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), but it cannot be used in other classes. It is important to refer directly to the class name. In fact, you need to understand that private means access permission restriction, and static means it can be used without instantiation, which is much easier to understand. The effect of adding other access permission keywords before static is also the same.
Member variables and member methods modified by static are customarily 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 code block modified with static represents a static code block. When the Java Virtual Machine (JVM) loads a class, the code block will be executed (very useful, haha).
1. static variable
Class member variables can be classified into two types according to whether they are static: one is a variable modified by static, called a static variable or class variable; the other is a variable not modified by static, called an instance variable.
The difference between the two is:
For static variables, there is only one copy in the memory (to save memory). The JVM only allocates memory once for static variables. The memory allocation of static variables is completed during the process of loading the class. It can be accessed directly by the class name (convenient). Of course, it can also be accessed through objects. Visit (but this is not recommended).
For instance variables, before an instance is created, memory will be allocated once for the instance variable. Instance variables can have multiple copies in the memory without affecting each other (flexible).
Therefore, static variables are generally used when the following two functions need to be implemented:
1).When sharing values between objects
2).When accessing variables is convenient
2. Static method
Static methods can be called directly through the class name, and any instance can also be called.
Therefore, the this and super keywords cannot be used in static methods, and the instance variables and instance methods of the class to which they belong cannot be directly accessed (that is, member variables and member methods without static). Only static member variables and member methods of the class to which they belong can be accessed.
Because instance members are associated with specific objects! This needs to be understood and figured out, not memorized! ! !
Because static methods are independent of any instance, static methods must be implemented and cannot be abstract.
For example, in order to facilitate method calling, all methods in the Math class in the Java API are static, and static methods within general classes also facilitate other classes to call the method.
A static method is a special type of method inside a class. The corresponding method is declared static only when needed. Methods inside a class are generally non-static.
3. static code block
A static code block is also called a static code block. It is a static statement block in a class that is independent of class members. There can be multiple and can be placed anywhere. It is 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 the order in which they appear in the class, and each code block will only be executed 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 generate method stubs} static{ Test5.a=5; System.out.println(a); } public void f(){ System.out.println("hhahhahah"); } }
Running results:
3
hhahhahah
1000
4
5
You can use static code blocks to assign values to some static variables. Finally, take a look at these examples. They all have a static main method, so that the JVM can call it directly without creating an instance when running the main method.
4. What do static and final mean when used together?
Static final is used to modify member variables and member methods, which can be simply understood as "global constants"!
For variables, it means that once a value is given, it cannot be modified and can be accessed through the class name.
For methods, it means they are not overridable and can be accessed directly through the class name.
Sometimes you want to define a class member so that its use is completely independent of any objects of that class. Normally, a class member must be accessed through an object of its class, but it is possible to create a member that can be used by itself without referencing a specific instance. Such a member can be created by adding the keyword static (static) before 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 reference 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 begins execution, it is declared static.
Variables declared static are essentially global variables. When an object is declared, a copy of the static variable is not produced, but all instance variables of the class share the same static variable. Methods declared static have the following restrictions:
1).They can only call other static methods.
2).They can only access static data.
3).They cannot refer to this or super in any way (the keyword super is related to inheritance and is described in the next chapter).
If you need to initialize your static variables through calculation, you can declare a static block. The Static block is only executed once when the class is loaded. The following example shows a class with a static method, some static variables, and a static initializer 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 is executed (printing a message), and finally, b is initialized to a*4 or 12. Then main() is called, main() calls meth(), passing the value 42 to x. The three println () statements refer to two static variables a and b, and the local variable x.
Note: It is illegal to reference any instance variables in a static method.
Here is the output of this program:
Copy the code code as follows:
Static block initialized.
x = 42
a=3
b = 12
Static methods and variables can be used independently of any object outside the class in which they are defined. This way, you just add the dot operator after the class name. For example, if you wish to call a static method from outside the class, you can use the following general format:
Copy the code code as follows:
classname.method()
Here, classname is the name of the class in which 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 followed by the dot operator. This is how Java implements a controlled version of global functions and global variables.
Below is an example. In main(), the static method callme() and the static variable b are accessed outside their classes.
class StaticDemo { static int a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } } class StaticByName { public static void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); } }
Here is the output of this program:
Copy the code code as follows:
a=42
b = 99
Static members cannot be accessed by instances created by their class.
If the members without static modification are object members, they are owned by each object.
Members modified with static are class members, which means they can be called directly by a class and are common to all objects.