Second, Java object-oriented (5) _static modifier

2018-04-29

The tree wants to be still, but the wind does not stop 

 

static modifier

 

The static modifier means static, and this modifier can modify fields, methods, and inner classes . The content modified by this keyword, in object-oriented, the content modified by static belongs to the class, not directly to the object, so the member variables modified by static are generally called class member variables, and the methods modified by static are generally called class member variables. class method.

 

Features of static modifier:

  1) Statically modified members (fields/methods) are loaded with the loading of the class in which they are located.

    When the JVM loads the bytecode into the JVM, the static-modified members already exist in memory.

  2) takes precedence over the existence of the object.

    Objects are created manually through the new keyword.

  3) satic modified members are shared by all objects of this type.

    Any object created from this class can access static members.

      Analysis: On the surface, to access static members through the object, its essence is still accessed using the class name, which has nothing to do with the object (you can see it through decompilation).

  4) Access static members directly using the class name.

    Because static-modified members directly belong to the class, not to the object, you can directly use the class name to access static members.

 

Access to class members and instance members

  1) Class members: Members modified with static belong directly to the class, and can access static fields and static methods through the class.

  2) Instance members: Members that are not modified by static belong only to objects, and non-static fields and non-static methods are accessed through objects (objects can actually access class members, but the bottom layer still uses the class name to access).

  3) In the static method: only static members can be called.

  4) Non-static methods: You can access static members and instance members.

 

The difference between static and non-static:

(1)

Non-static member variables are also known as instance variables.

Static member variables are also known as class variables. 

(2)

Non-static member variables exist when the object is created and disappear when the object disappears.

Static member variables exist when the class is loaded and disappear when the class disappears.

(3)

Non-static variables exist in object heap memory.

Static variables exist in the method area.

(4)

Non-static variables can only be called by objects.

Static variables can be called by class name or by object. 

 

 

When to define fields and methods as static:

  If this one state or behavior belongs to the whole thing (class), it is directly modified by static and is shared by all objects. In development, tool methods are often modified with static. If static modification is not used, these methods belong to objects of this class. We have to create objects first and then call the methods. In development, only one tool object is needed, and N objects may be created. At this time, we often design this class For a singleton, but still a bit cumbersome. Therefore, tools and methods are generally designed in development. In order to make the call simple, we use static modification.

 

Storage of static members in the JVM:

Code example:

 1 class Person
 2     {
 3         String name;//名字
 4         int age;//年纪
 5 
 6         static int totalNum = 5;//人类总人数5
 7 
 8         //构造函数
 9         Person(String n, int a){
10             name = n;//将n赋给name
11             age = a;//将a赋给age
12             totalNum++;
13         }
14 
15         void die(){
16             totalNum--;
17             System.out.println("死亡");
18         }
19 
20         //只有人类才有毁灭
21         static void destory(){
22             totalNum = 0;
23             System.out.println("人类毁灭");
24         }
25     }
26 
27 
28 //演示static修饰符
29 class PersonDemo2
30 {
31     
32         public static void main(String[] args) 
33         {
34             System.out.println(Person.totalNum);
35             Person p1 = new Person("张三",10);
36             Person p2 = new Person("李四",5);
37             System.out.println(Person.totalNum);//使用类调用对象
38 
39             p2.die();//p2死亡
40             System.out.println(Person.totalNum);
41 
42             Person.destory();
43             System.out.println(Person.totalNum);
44     }
45 }

 

 

输出结果:

 

Guess you like

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