Java static Keyword

Posted by devarticles on Sun, 26 May 2019 20:17:29 +0200

static keyword explanation of Java development, no more nonsense, directly on the code

First, template class, which includes: member variables, static variables, common code blocks, static code blocks, common methods, static methods, constructors, static internal classes (including: member variables, static variables, common code blocks, static code blocks, common methods, static methods, constructors).

package com.example;

public class Test1 {

    static int a = 10;
    static int b;
    int c = 50;
    int d;

    /**
     * code block
     */
    {
        System.out.println("{}:a = " + a);
        System.out.println("{}:b = " + b);
        System.out.println("{}:c = " + c);
        System.out.println("{}:d = " + d);
        a = 20;
        b = a + 10;
        c = b + 10;
        d = c + 10;
        System.out.println("{}:a = " + a);
        System.out.println("{}:b = " + b);
        System.out.println("{}:c = " + c);
        System.out.println("{}:d = " + d);
    }

    /**
     *Static code blocks can only assign and call static variables and static methods
     */
    static {
        System.out.println("static{}:a = " + a);
        System.out.println("static{}:b = " + b);
        a = 60;
        b = a + 10;
        System.out.println("static{}:a = " + a);
        System.out.println("static{}:b = " + b);

        metherdStatic();
    }

    /**
     * Constructor
     */
    public Test1() {
        System.out.println("Test1():a = " + a);
        System.out.println("Test1():b = " + b);
        System.out.println("Test1():c = " + c);
        System.out.println("Test1():d = " + d);
        a = 100;
        b = a + 10;
        c = b + 10;
        d = c + 10;
        System.out.println("Test1():a = " + a);
        System.out.println("Test1():b = " + b);
        System.out.println("Test1():c = " + c);
        System.out.println("Test1():d = " + d);
    }

    /**
     * Static methods: Only assign and call static variables and static methods
     */
    static void metherdStatic() {
        System.out.println("metherdStatic():a = " + a);
        System.out.println("metherdStatic():b = " + b);
        a = 100;
        b = a + 10;
        System.out.println("metherdStatic():a = " + a);
        System.out.println("metherdStatic():b = " + b);
    }

    /**
     * Common method
     */
    void metherd() {
        System.out.println("metherd():a = " + a);
        System.out.println("metherd():b = " + b);
        System.out.println("metherd():c = " + c);
        System.out.println("metherd():d = " + d);
        a = 100;
        b = a + 10;
        c = b + 10;
        d = c + 10;
        System.out.println("metherd():a = " + a);
        System.out.println("metherd():b = " + b);
        System.out.println("metherd():c = " + c);
        System.out.println("metherd():d = " + d);
    }

    public static void main(String[] strings) {
        System.out.println("main():Test1");
    }

    public static class ClassStatic {
        static int e;
        int f = 10;

        /**
         * code block
         */
        {
            System.out.println("{}:e = " + e);
            System.out.println("{}:f = " + f);
            e = 20;
            f = e + 10;
            System.out.println("{}:e = " + e);
            System.out.println("{}:f = " + f);
        }

        /**
         * Static code block
         */
        static {
            System.out.println("static{}:e = " + e);
            e = 20;
            System.out.println("static{}:e = " + e);
        }

        /**
         * Static method
         */
        static void metherdStatic() {
            System.out.println("metherdStatic():e = " + e);
            e = 100;
            System.out.println("metherdStatic():e = " + e);
        }

        /**
         * Common method
         */
        void metherd() {
            System.out.println("metherd():e = " + e);
            System.out.println("metherd():f = " + f);
            e = 100;
            f = e + 10;
            System.out.println("metherd():e = " + e);
            System.out.println("metherd():f = " + f);
        }
    }
}

II. Call Class

package com.example;

/**
 * Created by DELL on 2017/9/8.
 */

public class Test2 {

    public static void main(String[] strings) {
        /*---------------Ordinary class---------------------------------------------------------------------------*/
        /*Static method*/
        Test1.metherdStatic();

        /*Static variables*/
        System.out.println("Test1:a = " + Test1.a);
        System.out.println("Test1:b = " + Test1.b);

        /*Create examples*/
        Test1 test1 = new Test1();

        /*Member variables*/
        System.out.println("Test1:c = " + test1.c);
        System.out.println("Test1:d = " + test1.d);

        /*Common method*/
        test1.metherd();

        /*---------------Internal class*/
        /*Static method*/
        Test1.ClassStatic.metherdStatic();

        /*Static variables*/
        System.out.println("Test1.ClassStatic.e = " +        Test1.ClassStatic.e);

        /*Create examples*/
        Test1.ClassStatic classStatic = new Test1.ClassStatic();

        /*Member variables*/
        System.out.println("Test1.ClassStatic:f = " + classStatic.f);

        /*Common method*/
        classStatic.metherd();
    }
}

III. Printing results

//Static code block
static{}:a = 10
static{}:b = 0
static{}:a = 60
static{}:b = 70
//Static method
metherdStatic():a = 60
metherdStatic():b = 70
metherdStatic():a = 100
metherdStatic():b = 110
//Static methods called elsewhere
metherdStatic():a = 100
metherdStatic():b = 110
metherdStatic():a = 100
metherdStatic():b = 110
//Static variables called elsewhere
Test1:a = 100
Test1:b = 110
//When creating an instance, run the common code block
{}:a = 100
{}:b = 110
{}:c = 50
{}:d = 0
{}:a = 20
{}:b = 30
{}:c = 40
{}:d = 50
//When creating an instance, run the constructor
Test1():a = 20
Test1():b = 30
Test1():c = 40
Test1():d = 50
Test1():a = 100
Test1():b = 110
Test1():c = 120
Test1():d = 130
//Membership variables invoked elsewhere
Test1:c = 120
Test1:d = 130
//Common methods invoked elsewhere
metherd():a = 100
metherd():b = 110
metherd():c = 120
metherd():d = 130
metherd():a = 100
metherd():b = 110
metherd():c = 120
metherd():d = 130
//Static code blocks for internal classes
static{}:e = 0
static{}:e = 20
//Calling static methods of internal classes elsewhere
metherdStatic():e = 20
metherdStatic():e = 100
//Call static variables of static inner classes elsewhere
Test1.ClassStatic.e = 100
//When creating static internal class instances, run common code blocks
{}:e = 100
{}:f = 10
{}:e = 20
{}:f = 30
//Calling member variables of static inner classes elsewhere
Test1.ClassStatic:f = 30
//Calling common methods of static inner classes elsewhere
metherd():e = 20
metherd():f = 30
metherd():e = 100
metherd():f = 110

Four, summary

1. static modifies variables

Classification of class member variables according to whether they are static or not can be divided into two types:
< 1 > Variables modified by static are called static variables or class variables.
Variables that are not modified by static are called instance variables.

The difference between the two is as follows:
<1> Static variables have only one copy in memory (saving memory). JVM allocates one memory for static allocation. The memory allocation of static variables is completed in the process of class loading. It can be accessed directly by class names (convenience), and of course by objects (but this is not recommended).
<2> Every instance variable is created, memory is allocated to the instance variable once. There are multiple copies of the instance variable in memory, which do not affect each other (flexible).

Summary: In general, static variables can be used to implement the following two functions
< 1 > when objects share values directly;
When accessing variables conveniently.

2. static Modification Method

There are two classifications according to whether the classifications are static or not:
The method modified by static is called static method.
Variables that are not modified by static are called instance methods.

The difference between the two is as follows:
< 1 > Static methods can be invoked by class names, and any instance object can also be invoked.
< 2 > There can be no this and super keywords in static methods, which means that they cannot be overwritten and rewritten.
<3> Static methods can not directly access instance variables and instance methods of their classes, but can only access static member variables and static methods of their classes.
<4> Static methods are independent of any instance, so static methods must be implemented, not abstract.

3. static modifies code blocks

Classifying code blocks according to whether they are static or not can be divided into two types:
The method modified by static is called static code block.
Variables that are not modified by static are called common code blocks.

The difference between the two is as follows:
<1> Static code blocks are independent of class members in a class. They can be placed randomly. They are not executed by JVM when loading classes in any method body. If there are more than one static code blocks, the JVM will execute them sequentially according to the sequence in which they appear in the class, and each code block will only be executed once.
<2> In static code blocks, only static variables are assigned or called, only static methods can be invoked.

4. static Modified Classes

Note: static can only modify internal classes, not ordinary classes.

Classification can be divided into two categories according to whether the classes are static or not:
The method modified by static is called static inner class.
<2> Variables that are not modified by static are called internal classes or common classes.

The difference between the two is as follows:
<1> Static internal classes can have static variables, static methods, and of course, instance variables and instance methods.
<2> Static variables and static methods cannot exist in non-static internal classes.

Topics: jvm Java