Many students are learning or writing programs , Often see java A keyword in :static, And it's a question that interviewers often ask , It shows that this is very important , that static What's the function of keywords , How do we use it ? Let's take a look .

static The key words are as follows 4 Usage :

1. Decorate member variables

2. Decorated member method

3. Static code block

4. Static guide package

PS: Except for the one above 4 Usage ,java There's also a situation where static keyword , That's static inner class , But it's not common , We just need to introduce more , Let's mention it here , Just let us know that there is such a thing .

1. Decorate member variables

stay java in , cover static A member variable decorated by a keyword is also called a static variable , Common variables are called instance variables . Static variables are properties owned by a class , Can pass class name .+ How to access member variables ( When the modifier permission is sufficient ), Only in memory , Shared by all instance objects , Initialize on first class load . Instance variables are specific to each instance object , Cannot affect other instance objects .
Let's take a look : class Test { public static int count = 0; public int number = 0;
public Test(){ count++; number++; } } public class Test1{ public static void
main(String[] args) { System.out.println(Test.count);// output 0 Test test1 = new
Test(); Test test2 = new Test(); Test test3 = new Test();
System.out.println(test1.count);// output 3 System.out.println(test2.count);// output 3
System.out.println(test3.count);// output 3 System.out.println(test1.number);// output 1
System.out.println(test2.number);// output 1 System.out.println(test3.number);// output 1
System.out.println(Test.count);// output 3 } }

Because static variables are owned by a class , And shared by all instance objects of this class , So we create a new one at a time Test When object , Changed count It's all the same , however number Is instance variable , Every time you create a new , Each object affects its own member variables , Cannot affect member variables in other instance objects .

2. Decorated member method

cover static The method of keyword modification is also called static method . Static methods are also methods belonging to classes , Independent of specific instance objects , You can use the class name .+ Method name access ( When the modifier permission is sufficient ). The instance method must depend on the specific instance object to access . Because static variables and static methods belong to classes , Instance object is a concrete instance of this class , So instance methods can access static variables and static methods , But vice versa , because , Instance variables and instance methods depend on specific instances , Static methods do not depend on specific instances , So it is wrong to call instance variables and instance methods in static methods . as : When passing class name .+ Method name when static methods are called in this way , There is no specific instance at this time , Instance variables and instance methods cannot be called , So it compiles errors .
Let's take a look at specific examples : class Test { public static int count = 0; public int number = 0;
public Test(){} public static void print(){ System.out.println(" This is a static method ,count =
" + count); // call number Will report an error //System.out.println(" This is a static method ,number = " + number);
// call print1 Will report an error //print1(); } public void print1(){
System.out.println(" This is an example method ,count = " + count);
System.out.println(" This is an example method ,number = " + number); } } public class Test1{ public
static void main(String[] args) { Test.print(); Test test = new Test();
test.print(); test.print1(); } }


Got it , Let's take another look main method . Why? main There is a static Modifier ? When our program starts , At this time, no instance of any class has been created , So in java After the virtual machine has loaded our program , Only static methods can be called , That's why main The way is static Why .

3. Static code block

Static code block ready to use static{.....} Code contained , Can be used anywhere in the class ( Except in vivo ). Static code blocks are similar to static methods , Cannot access instance variables and instance methods , Static variables and static methods can be called . Only executed when the class is first loaded , If there are multiple static code blocks , Will be executed in order . Static code blocks take precedence over constructor execution , because ,static Will be executed when the class is loaded , The construction method executes when the instance is created .
Take an example : class Test { public Test(){ System.out.println(" Construction method "); } static{
print(); System.out.println(" Static code block "); } public static void print(){
System.out.println(" Static method "); } } public class Test1{ public static void
main(String[] args) { Test test = new Test(); } }

because static Code blocks are executed only once when the class is first loaded , We can often use this feature to optimize programs , For example, we need to read some pictures from the outside during class initialization , Documents, etc , We don't have to read from the outside every time we create an instance , Can be executed in static code blocks , This greatly optimizes performance . therefore ,static Code blocks are often used to perform initialization operations that only need to be performed once .

4. Static guide package
Usually when we call a static method of a class , All by class name .+ Method name to call , After the static guide package , We can call it directly .
Here's an example : package study; public class Test { public static void print(){
System.out.println("Hello World!"); } } package study; import static
study.Test.*; public class Test1 { public static void main(String[] args) {
print(); } }
After static import , We can call the method directly . be careful :1. After static guide package , Only static methods in the import class can be used , Instance methods cannot be called in this way .  
2. The called method cannot conflict with the existing method of the current class , That is, it cannot have the same name ( As long as the names are the same, they will be wrong ).

static The keyword is java A very important knowledge point in , I hope you can master it well . I hope this article can help you .

Technology
©2019-2020 Toolsou All rights reserved,
Unity Scene loading asynchronously ( Implementation of loading interface )ESP8266/ESP32 System : Optimize system startup time vue Of v-if And v-show The difference between JS How to operate college examination for the self-taught An overview of Marxism Faster RCNN Explanation of series algorithm principle ( note )CSS architecture design NOI2019 travels IAR Installation and use tutorial sort ( one ) bubble sort