static modifier

Static is a keyword that can be used to modify member variables , methods , inner classes and statement blocks .

① Things modified by static will be shared by all objects of the class .

②Things modified by static can be called using the class name (PS: It can also be called using the object name , but it is strongly recommended to use the class name to call .)

Loading with the loading of classes , static loading takes precedence over objects .

1. Static variables and static constants

Static variable declaration format :

( permission modifier ) ​​static data type variable name ; 

Static variables in java are shared by all objects of the class . When one object changes the value of the static variable, when other objects use the static variable again , its value is the changed value . (PS: static cannot modify local variables )

Static constant declaration format :

( permission modifier ) ​​static final data type constant name = constant value ; 

2. Static method

Static method declaration format :

( permission modifier ) ​​static data type method name () { 

method body ;

}

①In the static method :

Only static member variables and member methods can be called

without this

②In a non-static method :

Can call both static members and non-static members

3, static modified inner class

Static inner class declaration format :

( permission modifier ) ​​class Outer{

static class Inner{

}

}

The inner class modified by static can be used directly as an ordinary class , and it is not necessary to create an instance of the outer class before it can be referenced like ordinary inner classes.

4, static modified statement block

Static statement block declaration format :

static {

statement body ;

}

Static statement blocks are typically used for required initialization operations

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324836178&siteId=291194637