当前位置:网站首页>Deep understanding of static keyword
Deep understanding of static keyword
2022-07-25 09:25:00 【Work hard, work hard, gzc】
outline :static Modifiers have four functions in general : Decorate a field or method 、 Decorated code block 、 Static import package 、 Construct static inner classes . In fact, my understanding static It is for convenience that we do not create instances of classes , You can call a method or variable directly through the class name
One 、 Decorate a field or method
1. Decorate fields
When using static When modifying a field , Then this field is a static field , There is only one such field in each class , Namely be static Decorated variables are shared by all instances of the class , Other non static fields , Each instance of this class has its own copy .
for instance : In the student class Student There are two fields in , Namely : School id——schoolId( Static field )、 Student id——studentId( Non static fields ). Every Student Objects have their own students id, But these students all have the same school id.
in other words , If there is 1000 A student object , Then there are 1000 A student id, Corresponding to each student object . however , There is only one static field school id, Even if there is no student object , Static field school id There is also .
Static variables are used less , Generally, static constants are used more . for example ,Java Of Math Class Π static const .
public static final double PI = 3.14159265358979323846;
2. Modification methods
By static The method of decoration , It's called a static method . Static methods are methods that can be executed without instantiating objects . When calling a normal method , After instantiating the object , Call this method through this object ; But when you call a static method , There is no need to instantiate the object , because static The method of decoration belongs to class , Call this method directly through the class name .
matters needing attention :
After being defined as a static method , Methods can no longer be used this Keyword. ;
Non static methods cannot be used in static methods , But static methods can be called in non static methods ;
for example :
Math Class max Method , Pass in two integer values , Return maximum , Expression for :
Math.max(int a, int b)// Use Math Class directly calls max The method can , No need to instantiate objects
Scenarios using static methods : Method only needs to access the static field of the class or it is used when the calling method does not need to instantiate the object .
Two 、 Decorated code block
If the static field of a class needs complex initialization code , You can use static code blocks .
Put the code in a block , And mark the block as static, Then a static code block is constructed .
// Randomly generate a value less than 100 The integer of
private static int id = 1;
static{
Random random = new Random();
id = random.nextInt(100);
}
3、 ... and 、 Static import package
When using static fields or methods , You need to add the class name before the field or method , For example, when using the method of comparing the maximum value in the mathematical class , Need to be in max Methods before adding Math Class name , When static fields or methods are used less , There's no problem with that , But if more static fields or methods are used , You can statically import packages in the header of the class file .
for example :
Before and after the static import package , Use the maximum comparison function , How to write :
// The head is not statically introduced Math class
Math.max(1,2);
// Static head introduces Math class
import static java.lang.Math.*;
max(1,2);
// Introduce classes statically , Can make the code clearer , The amount of code is also reduced
Four 、 Construct static inner classes
When an inner class is just to hide inside a class , When there is no need for the internal class to have a reference to the external class , You can declare an inner class as static.
matters needing attention :
As long as the inner class does not need to access the outer class object , Then you should use static inner classes
Unlike regular inner classes , Static inner classes can have static fields and methods
The inner class declared in the interface is automatically static and public
边栏推荐
- Mongodb installation and use
- 动态添加多tab,并初始化每个tab页面
- 无法再web服务器上启动调试,web服务器未能找到请求资源
- Redis安装(Ubuntu)
- CentOS changes MySQL database directory
- Ctfhub skill tree Web
- Guangzhou has carried out in-depth "100 day action" to check the safety of self built commercial houses, and more than 2 million houses have been checked in two months
- API健康状态自检
- MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
- 『怎么用』观察者模式
猜你喜欢
随机推荐
Arrange the array into the smallest number
centos更改mysql数据库目录
[common tools] obtain system status information based on psutil and gputil
C#语言和SQL Server数据库技术
Mongodb installation and use
sqli-labs Basic Challenges Less1-10
Reverse Integer
activemq--可持久化机制之AMQ
[WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]
activemq--可持久化机制之JDBC代码
PHP介绍
How can technologists start their personal brand? Exclusive teaching of top five KOLs
C#语言和SQL Server数据库技术
Mongodb exploration phase [easy to understand]
[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line
uni-app - Refused to display ‘xxx‘ in a frame because an ancestor violates the following Content Sec
Analysis of concat and group in MySQL_ Use of concat
C language and SQL Server database technology
idea实用tips---如今将pom.xml(红色)改为pom.xml(蓝色)
activemq--异步投递









