static members in Java classes and objects“

1, On Students

First, you can find out why there is a static keyword:

public class Student2 {
    String name;
    String gender;
    int age;

    String classRoom="326";

    public Student2(String name, String gender, int age){
        this.name=name;
        this.gender=gender;
        this.age=age;
    }

    public void printStudentInfo(){
        System.out.println(name+","+gender+","+age);
    }

    public static void main(String[] args) {
        Student2 s1=new Student2("LiLei","male",13);
        Student2 s2=new Student2("HanMeiMei" ,"female",12);
        Student2 s3=new Student2("Jim","male",13);
    }
}

There are three different objects:

In fact, our careful analysis is somewhat unreasonable, in which the student's name, gender and age are strongly related to the student and describe the student. The classroom has nothing to do with describing students.

This raises questions:

2, static modifier member variable

Static modified member variables are called static member variables. The biggest feature of static member variables is that they do not belong to a specific object and are shared by all objects.
Static member variable properties:
1. It does not belong to a specific object, but is a class attribute. It is shared by all objects and is not stored in the space of an object.

We can verify that after debugging in debug mode, we can find the results as shown in the following figure:

However, it should be noted that static member variables are not stored in specific objects and are shared by all objects. verification:

The print result is:

It can also be proved by printing the following (hashCode can be simply understood as the address of the current object)

The print result is:

The result is the same, so the side proves that there is only one static member variable.

2. It can be accessed through both object and class name, but it is generally recommended to use class name.

So are they accessing the same content?

    public static void main(String[] args) {
        Student2 s1=new Student2("LiLei","male",13);
        Student2 s2=new Student2("HanMeiMei" ,"female",12);
        Student2 s3=new Student2("Jim","male",13);

        System.out.println(s1.classRoom.hashCode());
        System.out.println(s2.classRoom.hashCode());
        System.out.println(s3.classRoom.hashCode());

        System.out.println(Student2.classRoom.hashCode());
    }

The result is:

So it's the same.
3.JDK7 and before, HotSpot (Java virtual machine) is stored in the method area, and JDK8 and after, class variables are stored in the Java heap.
4. The life cycle follows the life of the class (that is, it is created with the loading of the class and destroyed with the unloading of the class)

3, static modifier member method

If you change the modification of classRoom to private:


Then write a TestStudent2 in the same package:

public class TestStudent2 {
    public static void main(String[] args) {
        System.out.println(Student2.classRoom);
    }
}

Compilation failed because classRoom is private in Student2 class. So what if you want to compile? You can write a public method:

public String getClassRoom(){
    return classRoom;
}

But it turns red,

Note: if public methods want to operate on static variables, they must also be decorated with static

    //Static member method
    public static String getClassRoom(){
        return classRoom;
    }

You can also create objects:

Student2 s=new Student2("111","male",13);
s.getClassRoom();

So how should the static attribute be accessed?
In Java, the member method modified by static is called static member method. It is a class method, not unique to an object. Static members are generally accessed through static methods.
Static method properties:
1. It does not belong to a specific object, but is a class method.

2. It can be called by object or class name. Static method name (...), and the latter is more recommended.
3. The static method has no hidden this reference parameter, so you cannot access any non static member variables in the static method.
verification:

The example method:

With this feature, some operations cannot be performed, such as:

Instance variables are accessed through this, but there is no this in static member methods.
4. no static method can be invoked in the static method, because the this parameter in the non static method can not pass the this reference when invoking in the static method.

    public static String getClassRoom(){
        printStudentInfo();
        return classRoom;
    }

Compilation failed:

Note: in the instance method, you can directly access static members without error.

    public void printStudentInfo(/*Student2 this*/){
        System.out.println(getClassRoom());
    }

4, static member variable initialization

Note: static member variables are generally not initialized in the construction method, which initializes the instance properties related to the object. The initialization of static member variables can be divided into two types: in place initialization and static code initialization.

1. Local initialization

Local initialization means that the initial value is given directly in the definition.

String name;
String gender;
int age;
private static String classRoom="326";

2. Static code block initialization

What is a code block? Please look at the next one.

Tags: Java

Posted on Wed, 29 Sep 2021 19:23:48 -0400 by elis