Understand one article java-static keyword

Classes with static methods are usually not intended to be initialized . We all know that abstract classes cannot be initialized . But when you want some non abstract classes to be initialized , You can use private constructors to restrict .Math This is how classes prevent themselves from being initialized . It makes the constructor private , So you can't create Math Examples of .

But that doesn't mean that classes with static methods can't be initialized . in fact , As long as there is main() The classes of are all static methods ! So you can combine static and non-static methods in your class at will .

Static methods cannot call non static variables and methods .

For example, the following code :
public class StaticTest { int i=0; public int geti(){ return i; } public
static void main(String[] args) { System.out.println(i);// Wrong , int o=geti();// Wrong ,
} }
Static variables are shared by all instances of the same class .

Let's look at the following example :
public class StaticTest { int i=0; public int geti(){ return i; } public
static void main(String[] args) { Song song01=new Song("lalala"); Song
song02=new Song("hahaha"); Song song03=new Song("wawawa");
System.out.println(song01.toString()+"\n"+song02.toString()+"\n"+song03.toString());
} } class Song { private String name; private int num=0; Song(String name) {
this.name=name; num++; } @Override public String toString() { return name+"
"+num; } }
The output is :

lalala 1

hahaha 1

wawawa 1

We can find out song01,song02,song03 Each has one name, But there's only one of them num variable . So , Static variables are shared . All instances of the same class share a static variable .

Static variables are initialized when the class is loaded . Similar to the following code :

 
public class StaticTest { public static void main(String[] args) {
// Static variables are initialized before any object of the building class is ready // The partial state variable will be in any Initialization before static method execution
System.out.println(Song.num); Song song01=new Song("lalala");
System.out.println(Song.num); } } class Song { private String name; public
static int num=0;//song When class is loaded num Is initialized to 0 Song(String name) { this.name=name;
num++; } }
 

Output after program running :

0

1

 

Class property static Variables referenced , Will be used as GC Of root Root node . Being the root node means , This kind of variable is basically not recycled . therefore ,static It is easy to introduce the risk of memory leakage .

When an interviewer asks you to explain static keyword , If you say
static Attributes can be decorated , Methods and inner classes , And what's the effect of decoration , So the interviewer basically doesn't remember your answer , The whole impression is ordinary .

But if you finish , To add , You've had a memory leak problem , that is because static A decorated one Map Caused by a variable of type , Finally, we checked the stack information and found the problem , And solved the problem . that , The interviewer's inner impression of you at this time , It will be unnaturally promoted .

about static, A deeper understanding is ,static The referenced property , Methods and inner classes , Directly generate reference relationship with class , Instead of an instance of a class . That's why , You can use the class name . attribute , Class name . Method and class name . Internal class name , To directly refer to a static Decorated attribute , Method or inner class .
If you don't static modification , Then you have to use instances to refer to these methods , Property or inner class , The most typical is the inner class .

Let's take a look at the following short code :
public class StaticTest { public static void main(String[] args) { Song.get();
//Song.set(); Song song=new Song(); song.set("lili"); } } class Song { private
String name; public void set(String name){ this.name=name; } public static void
get(){ System.out.println("name"); } }

We defined a Song class , A non static method is defined in it set() And a static method get(), When we want to execute static When modifying static methods, we can directly use [ class . Method name ] To call this method , And when we want to call set() Method time , We have to new One Song Examples of , Then use [ Instance name . Method name ] To call this method , This is it. static The basic role of .

 

Let's simply modify the code :
public class StaticTest { public static void main(String[] args) { Song.get();
//Song.set(); Song song=new Song(); song.set("lili"); Song.Singer singer=new
Song().new Singer(); singer.set("fengfeng"); } } class Song { private String
name; public void set(String name){ this.name=name; } public static void get(){
System.out.println("name"); } class Singer{ private String singername; public
void set(String singername){ this.singername=singername; } } }
 

 

I believe many programmers are curious , Why hasn't one been static Decorated inner class , You have to say that :

OutterClass.InnerClass innerClass = new OutterClass().new InnerClass();

That's what we did :

Song.Singer singer=new Song().new Singer();

Explain , This is
Because you didn't use it static modification InnerClass, So you have to new Come out with one OutterClass Examples of , On this basis new Out instances of inner classes , Because an inner class can only be referenced by an instance of an outer class . If you use static modification , So you can use inner classes like this .

OutterClass.StaticInnerClass staticInnerClass = new
OutterClass.StaticInnerClass();

Like this :

 
public class StaticTest { public static void main(String[] args) {
Song.get(); //Song.set(); Song song=new Song(); song.set("lili"); //Song.Singer
singer=new Song().new Singer(); //singer.set("fengfeng"); Song.Singer
singer=new Song.Singer(); } } class Song { private String name; public void
set(String name){ this.name=name; } public static void get(){
System.out.println("name"); } static class Singer{ private String singername;
public void setSinger(String singername){ this.singername=singername; } } }

The biggest difference between the two methods is , The first way , If you want to get InnerClass Examples of , You have to have one OutterClass Examples of , In fact, you created two instances in this way , So there are two new keyword . And the second way is easier to understand , Static inner classes do not depend on instances of outer classes , So you just need to create an instance of the inner class directly , So there's only one new keyword .

Technology
©2019-2020 Toolsou All rights reserved,
JAVA Detailed explanation of anomalies MySQL An interview is a must ! How to use it quickly html and css Write static page R Language cluster analysis case Dialogue between apple and Nissan suspended ,Apple Car How's it going ?java Realize the function of grabbing red packets SpringBoot practice ( five ):mybatis-plus In BaseMapper,Iservice and ServiceImpl Google says home office affects work efficiency !2021 Return to offline office in 2010 about keras use fit_generator Encountered in StopIteration Programmer Tanabata Valentine's Day confession code