The purpose and usage of the static keyword in Java are described in detail

  • 2020-04-01 03:41:02
  • OfStack

Static means "global" or "static". It is used to modify member variables and methods, and can also form static static blocks of code, but there is no concept of global variables in the Java language.

Member variables and methods that are treated as static are independent of any object in the class. That is, it is not dependent on a class-specific instance and is Shared by all instances of the class.

As soon as the class is loaded, the Java virtual machine can locate it in the method field 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 reference to any object.

The static member variable and member method modified by public are global variables and global methods in essence. When the object city 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.

The static variable can be preceded by a private modifier, which means that the variable can be used in a static block of code of a class, or in other static member methods of the class (you can also use the waddle in non-static member methods, of course), but it is important that it is not directly referenced by the class name in other classes. In fact, you need to understand that private is an access limit, and static means you can use it without instantiating it, which makes it a lot easier to understand. Static with other access keys before the effect of the same analogy.

The member variables and methods of static modification are customarily called static variables and static methods. They can be accessed directly by the class name. The access syntax is:

Class name. Static method name (parameter list...)

Class name. Static variable name

A static block of code is a block of static code that is executed when the Java virtual machine (JVM) loads a class.

1. Static variable

Class member variables can be classified into two types according to whether they are static or not. The other is a variable that is not static, called an instance variable.

The difference between the two is:

For static variables with only one copy in memory (to save memory), the JVM only allocates memory statically once. The allocation of memory for static variables is done during the loading of the class, which can be accessed directly by the class name (convenient), or of course by object (which is not recommended).

For instance variables, memory is allocated to instance variables once without an instance being created, and instance variables can have multiple copies in memory without affecting each other (flexible).

Therefore, static variables are generally used when the following two functions are required:

1). When sharing values between objects
2). When it is convenient to access variables

2. Static methods

Static methods can be called directly from the class name, any instance can be called,

Therefore, the keyword "this" and "super" cannot be used in static methods, and the instance variables and methods of the class can not be directly accessed (that is, the member variables and methods without static), but only the static member variables and methods of the class can be accessed.

Because instance members are associated with specific objects! This need to understand, want to understand the truth, not memory!!

Since a static method is independent of any instance, a static method must be implemented, not abstract abstract.

For example, in order to facilitate method invocation, all methods in the Math class in the Java API are static, and the static methods in the general class are also convenient for other classes to invoke the method.

Static methods are a special class of methods, only when the corresponding method is needed to declare the static, a class of methods are generally non-static

3. Static code block

Static block of code is also called static blocks of code, it is independent of the members of the class in the class of the static block, there can be multiple, position can be literally put, it is not in any way in the body, the JVM will perform these static loading class code block, if there are multiple static code block, the JVM will order in which they appear in the class of execute them in sequence, each block of code will be executed only once. Such as:


public class Test5 {  
private static int a;  
private int b;  

static{  
Test5.a=3;  
System.out.println(a);  
Test5 t=new Test5();  
t.f();  
t.b=1000;  
System.out.println(t.b);  
}  
static{  
Test5.a=4;  
System.out.println(a);  
}  
public static void main(String[] args) {  
//TODO automatically generates method stubs
}  
static{  
Test5.a=5;  
System.out.println(a);  
}  
public void f(){  
System.out.println("hhahhahah");  
}  
}

Operation results:
3
hhahhahah
1000
4
5

Static blocks of code can be used to assign values to static variables. At last glance, each of these examples has a static main method, so that the JVM can call the main method directly when it runs without creating an instance.

4. What is the static and final block

Static final is used to modify member variables and methods.

For variables, this means that once a value is given, it cannot be modified and is accessible through the class name.

For methods, it is not overridden and can be accessed directly through the class name.

Sometimes you want to define a class member whose use is completely independent of any object in the class. Typically, a class member must be accessed through the object of its class, but you can create a member that can be used by itself without referring to a specific instance. You can create such a member by prefixing the declaration of the member with the keyword static. If a member is declared static, it can be accessed before any object of its class is created without reference to any object. You can declare both methods and variables static. The most common example of a static member is main(). Because you must call main() at the beginning of the program, it is declared static.

A variable declared static is essentially a global variable. When you declare an object, instead of making a copy of the static variable, all the instance variables of the class share the same static variable. Methods that declare static are limited by the following:

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 variable by evaluating it, you can declare a static block that executes only 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 executes (printing a message), and finally, b is initialized to either a*4 or 12. Then it calls main(), and main() calls meth(), passing the value 42 to x. The three println () statements refer to the two static variables a and b, as well as 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 of the class in which they are defined, static methods and variables can be used independently of any object. In this way, you simply add a number operator after the name of the class. For example, if you want to call a static method from outside the class, you can use the following general format:

classname.method( )

In this case, classname is the name of the class in which you define a static method. As you can see, this format is similar to that of calling a non-static method through an object reference variable. A static variable can be accessed in the same format -- the class name dotted operator. This is how Java implements a controlled version of global functionality and global variables.

Here's 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 the program:


a = 42
b = 99

A static member cannot be accessed by the instance created by its class.

If the member without static is an object member, it is owned by each object.

Static modifier is a member of the class, that is, can be directly called by a class, for all objects in common.


Related articles: