Static/non-static variables and methods are fundamental concepts in Java development. This article will detail the definitions of these concepts and their distinct relationships from the aspects of class life cycle, JVM memory location, and interuse relationships.

First, a brief mention of the life cycle of a class

I. The life cycle of a class

From the moment a class is loaded into memory until it is unloaded from memory, the class life cycle consists of seven processes: loading, validating, preparing, parsing, initializing, using, and unloading. By the time initialization is complete, the contents of the class’s.class bytecode are loaded into the JVM’s method area, and the class’s static variables are initialized.

Static member

A static variable

Variables that are modified static are called static variables or class variables. Class variables belong to the entire class and can be called directly by the class name or by all instantiated objects created by the class.

  • During class loading, memory is allocated and “zero” is assigned in the JVM method area during the preparation phase.
  • During the initialization phase of class loading, the class is initialized according to the value of the Java program code defined in the class.
  • Class variables shared by all objects reside in the same block of memory in the method area, and changes made by objects to class variables affect the use of other objects.
  • The life cycle of a static variable is basically the life cycle of a class, created as the class is loaded and lost when the class is unloaded.

A static method

Methods that are static are called class methods. Class methods belong to the entire class and can be called directly by the class name, by instance objects, or by instance methods.

  • During class loading, the bytecode of a class method is loaded into the method area of the JVM during the loading phase.
  • Class methods are loaded into memory and assigned entry addresses.
  • The life cycle of a class method is also roughly equal to the life cycle of the class.

Non-static member

Nonstatic variable

A static variable is a member variable that is instantiated by a class and can only be called by objects created by the class.

  • During the usage phase of the class lifecycle, the member variables are allocated memory in the JVM heap space during the instantiation of the object by the class.
  • Different member variables are allocated to different heap Spaces. Changes in one object’s member variables do not affect those of other objects.
  • The life cycle of a member variable is equal to the life cycle of an object, created as the object is created and released as the object is reclaimed.

Nonstatic method

Methods that are not static are called instance methods. Objects instantiated by a class can only be called by objects created by the class.

  • During class loading, the bytecode of a class method is loaded into the method area of the JVM during the loading phase.
  • During the usage phase of the class lifecycle, instance methods are assigned entry addresses only after the class is first instantiated.
  • All object instances share an instance’s entry address, but incoming object instances are in different heap memory Spaces.
  • The instance method entry address is cancelled after the class’s objects have been reclaimed.

Four. Call each other

Static members and non-static members call each other. There are roughly 16 kinds as follows by permutation and combination. The corresponding call relationship will be analyzed one by one based on the above content and IDEA

1. Class variables Invoke class variables

OK, but there are order requirements, because class variables are initialized one by one during initialization. Otherwise, IDEA will prompt Illegal forward reference error. More specifically, during the execution of the class constructor < Clinit > () method during the initialization phase of the class, the < Clinit > () method is generated by combining the compiler’s automatic collection of assignment actions for all class variables in the class with statements in the ** static statement block (static{} block) **. The order in which the compiler collects is determined by the order in which statements appear in the source file. Therefore, the class static variable defined first cannot access the class static variable defined later.

    public static int staticInt0 = 0;
    public static int staticInt = staticInt0;
Copy the code

2. Class variables call class methods

OK, class methods are the loading phase of the class loading process that is loaded into the method area, and class variable initialization is the initialization phase after the class method loading.

    public static int staticInt = staticMethod();
	
    private static int staticMethod() {
        System.out.println("StaticMethod is called~");
        return 1;
    }
Copy the code

3. Class variables call instance variables

Error, because instance variables are allocated during object instantiation during the use phase of the class life cycle, later than when class variables are initialized during initialization, they cannot be called.

4. Class variables call instance methods

Error, because the instance method has been loaded into the method area of the JVM when the class variable is initialized, but the instance method has not been allocated an entry address, so it cannot be accessed.

5. Class methods call class methods

OK, even if it’s not in the same order. This is fine at class loading and runtime.

    public static int staticMethod() {
        System.out.println("StaticMethod is called~");
        return staticMethod2();
    }

    public static int staticMethod2() {
        System.out.println("StaticMethod2 is called~");
        return 2;
    }
    public class ClassMemberTest {
    public static void main(String[] args) {
        System.out.println("ClassMemberDemo.staticInt:"+ staticMethod()); }}Copy the code

Run time output

StaticMethod is called~
StaticMethod2 is called~
ClassMemberDemo.staticInt:2
Copy the code

Just to be clear, what happens if staticMethod() and staticMethod2() call each other? I’ll leave it to the reader to test.

6. Class methods call class variables

OK, the runtime calls the class method by the class name. If the class has not been loaded, it loads the class first, initializes the class variable, and then calls the class method, which accesses the class variable inside the class method.

    public static int staticInt = 1;

    public static int staticMethod() {
        System.out.println("StaticMethod is called~");
        return staticInt;
    }
    
    public class ClassMemberTest {
    public static void main(String[] args) {
        System.out.println("ClassMemberDemo.staticInt:"+ staticMethod()); }}Copy the code

Run time output

StaticMethod is called~
ClassMemberDemo.staticInt:1
Copy the code

7. Class methods call instance methods

Error, failed to compile. The reason for this is that the class method may be called where the instance method has not yet been assigned an entry, such as when a class variable is initialized and the class is first used when the class method is used.

Class methods call member variables

Error refers to the use of member variables in the body of a class method for reasons similar to 3.

9. Call a member variable

OK, but there are order requirements. Because when an object is instantiated, instance variables are also ordered.

    private int nullStaticInt ;

    private int nullStaticInt2 = nullStaticInt;
Copy the code

10. Member variables call instance methods

OK, member variables are initialized when the object is instantiated, and the instance method has already been assigned an entry address.

    public int nullStaticInt = nullStaticMethod();

    private int nullStaticMethod() {
        return 2;
    }
Copy the code

11. Member variables call class variables

OK, because instance variables are allocated during object instantiation during the use phase of the class life cycle, and the class variables are already initialized during initialization.

12. Member variables call class methods

OK, for the same reason as above, the bytecode of the class method is already loaded into the METHOD area of the JVM during the load phase.

13. Instance methods call instance methods

OK, even if the order is inconsistent, the code compiles and the bytecode loads into memory fine. It’s just that when they call each other, there’s a problem. The specific problems are the same as 5.

14. Instance methods call member variables

OK, this is a normal common scenario.

15. Instance methods call class variables

OK, OK here because the instance method is called at run time through the object. Class variables are later than instance methods, though, from the point of loading into memory.

16. Instance methods call class methods

OK, here’s reason 10.

Here from the class life cycle, JVM memory location and mutual use relationship comb Java class static/non-static member/function comb introduction completed, feel useful click blue ~ (· ω ·) blue