In
Java
, members modified by
static
are called static members or class members, which do not belong to a specific pair of
The image is shared by all objects
.
static modifier member variable
Static
modified member variables, called static member variables
[Static member variable characteristics]
1.
It does not belong to a specific object, it is an attribute of a class, shared by all objects and not stored in the space of an object
2.
It can be accessed either through the object or through the class name, but it is generally recommended to use the class name to access
3. JDK7
and earlier,
HotSpot (Java
virtual machine
)
is stored in the method area,
JDK8
and later, class variables are stored in the
Java
heap
4.
Class variables are stored in the method area
5.
The life cycle
is created with the loading of the class and destroyed with the unloading of the class
define a student class
class Student{
public String name;
public String sex;
public static String classes;
}
public class classDemo {
public static void main(String[] args) {
Student.classes="104Java班";
Student student1=new Student();
student1.name="zhangsan";
Student student2=new Student();
student2.name="lisi";
Student student3=new Student();
student3.name="wangwu";
}
}
The above three objects student, although their names are different, the static member variables indicate that they belong to the same class
Does not belong to a single object,
static member variables are not stored in a specific object.
static modified member method
A member method modified by
static
is called a static member method, which is a method of a class, not specific to an object
. Static members are generally accessed through static methods.
public class Student{
private static String classRoom = "Bit306";
public static String getClassRoom(){
return classRoom;
}
}
public class TestStudent {
public static void main(String[] args) {
System.out.println(Student.classRoom);
}
[
Static method characteristics
]
1.
It does not belong to a specific object, it is a class method
2.
It can be called through the object, or through the class name
. The
static method name
(...)
method, the latter is more recommended
3.
Static methods have no hidden
this
reference parameter, so no non-static member variables can be accessed in static methods
4.
No non-static methods can be called in static methods, because non-static methods have
this parameter, and
this reference
cannot be passed when calling in static methods
static member variable initialization
Note: Static member variables are generally not initialized in the constructor, and the object-related instance properties are initialized in the constructor.
1.
In-place initialization
private static
String
classRoom
=
"Bit306"
;
2.
Static code block initialization
code block
A piece of code defined with
{}
is called a code block
. According to the position and keywords defined by the code block, it can be divided into the following four types:
Normal code block: A code block defined in a method
.
Constructed code block:
A code block (
without modifiers
)
defined in a class
. Also called:
instance code block
.
Constructor blocks are generally used to initialize instance member variables
.
Instance blocks are executed in preference to constructors because after compilation, the compiler copies the code in the instance blocks before the first statement of each constructor. Instance code blocks are only executed when the object is created.
Static code block: A code block defined using
static
is called a static code block.
Generally used to initialize static member variables
1. No matter how many objects are generated in a static code block, it will only be executed once
2. Static member variables are properties of the class, so they are opened up and initialized when the
JVM
loads the class
3. If a class contains multiple static code blocks, when compiling the code, the compiler will merge them in the order defined, and finally put them in the generated <>
method, which is called when the class is loaded, and only calls once.
Synchronized code block