List of articles
-
-
-
- 1.Static Variable
- 2.Static Method
- 3.Static import Statement
- 4.Static Block
- 5.Static Class
- 6.Summary
-
-
1.Static Variable
Use static
Keyword to declare a static variable , The format is as follows :
Access right static data type Variable name
for example : Declare a static variable of integral type
public static Integer staticVar;
By static
The decorated variable belongs to a class , This means that it can only have one copy of the variable at run time .
When you define a static variable in a class , Each instance of the class has access to the copy . Individual instances of classes do not have their own local copies , Just as they do with non static variables .
public class JavaStaticExample
{
public static void main(String[] args)
{
DataObject objOne = new DataObject();
objOne.staticVar = 10;
objOne.nonStaticVar = 20;
DataObject objTwo = new DataObject();
System.out.println(objTwo.staticVar); //10
System.out.println(objTwo.nonStaticVar); //null
DataObject.staticVar = 30; //Direct Access
System.out.println(objOne.staticVar); //30
System.out.println(objTwo.staticVar); //30
}
}
class DataObject {
public static Integer staticVar;
public Integer nonStaticVar;
}
Output is :
10
null
30
30
Above , Make the static variable value more 30, The output values are also 30.
in addition , You can also find access to static variables , There is no need to create any instances , This shows that static
Variables belong to class scope .
2.Static Method
Declaration format :
Access right static Return type Method name
for example , One Integer
Type of public static
Variable of type
public static Integer staticVar;
public static Integer getStaticVar(){
return staticVar;
}
Be careful :
-
Only static variables in static methods can be accessed , If you try to access a non static variable , The compiler will report an error :
Cannot make a static reference to the non-static field nonStaticVar“.
-
Static methods can be accessed through class references , There is no need to create an instance of a class , Although you can also use instance references to access , But access through class references , But compared to accessing through class references , There is no difference between the two .
-
Static methods belong to class scope
public class JavaStaticExample
{
public static void main(String[] args)
{
DataObject.staticVar = 30; // Direct access
Integer value1 = DataObject.getStaticVar(); // Access through class
DataObject objOne = new DataObject();
Integer value2 = objOne.getStaticVar(); // Access through class instances
System.out.println(value1);
System.out.println(value2);
}
}
class DataObject
{
public Integer nonStaticVar;
public static Integer staticVar; // Static variables
public static Integer getStaticVar(){
return staticVar;
}
}
Output :
30
30
3.Static import Statement
The normal import declaration imports classes from the package , So you can use them without package references . Similarly , Static import declaration imports static members from a class , And allow them to be used without class references .
There are also two forms of static import statements : Single static import and static on demand import .
A single static import declaration imports a static member from a type . The static on demand import declaration imports all static members of a type .
//Single-static-import declaration: Single import
import static <<package name>>.<<type name>>.<<static member name>>;
//Static-import-on-demand declaration: Import all
import static <<package name>>.<<type name>>.*;
// Static import declaration
import static java.lang.System.out;
public class JavaStaticExample
{
public static void main(String[] args)
{
DataObject.staticVar = 30;
out.println(DataObject.staticVar);
}
}
class DataObject
{
public static Integer staticVar;
}
Output :
30
4.Static Block
Static blocks are part of class initialization , use static
Keyword encapsulation
public class Main {
//static initializer
static {
System.out.println("Inside static initializer");
}
}
Static blocks are executed when a class is loaded into memory , A class can have multiple static blocks , These static blocks are executed in order as defined in the class .
import static java.lang.System.out;
class DataObject
{
public Integer nonStaticVar;
public static Integer staticVar; //static variable
//It will be executed first
static {
staticVar = 40;
//nonStaticVar = 20; // Cannot manipulate non static members
}
//It will be executed second
static {
out.println(staticVar);
}
}
Output :
40
5.Static Class
stay Java in , You can use static classes as inner classes . Like other static members , Nested classes belong to class scope , So you can access internal static classes without external class objects .
public class JavaStaticExample
{
public static void main(String[] args)
{
//Static inner class example
System.out.println( DataObject.StaticInnerClas.innerStaticVar );
}
}
class DataObject
{
public Integer nonStaticVar;
public static Integer staticVar; //static variable
static class StaticInnerClas {
Integer innerNonStaticVar = 60;
static Integer innerStaticVar = 70; //static variable inside inner class
}
}
Static inner classes cannot access non static members of outer classes . It can only access static members of external classes .
public class JavaStaticExample
{
public static void main(String[] args)
{
DataObject.StaticInnerClas.accessOuterClass();
}
}
class DataObject
{
public Integer nonStaticVar;
public static Integer staticVar; //static variable
static {
staticVar = 40;
//nonStaticVar = 20; //Not possible to access non-static members
}
public static Integer getStaticVar(){
return staticVar;
}
static class StaticInnerClas
{
public static void accessOuterClass()
{
System.out.println(DataObject.staticVar); //static variable of outer class
System.out.println(DataObject.getStaticVar()); //static method of outer class
}
}
}
Output :
40
6.Summary
About static
The key words are summarized as follows :
static
Members belong to a class , You don’t need to create a class instance to accessstatic
member- Static members ( Variables and methods ) Can only be accessed in static methods and static blocks .
- Non static members cannot be in static methods , Blocks and inner classes
- A class can have multiple static blocks , Execute in the order defined in the class
- A class can be static only if it is an inner class in an outer class
- Static import can be used to import all static members of a class , These members can be referenced without any class references