JavaState modifier final & static

Table of Contents

final modifies our member methods, member variables, and classes

Sample code:

final modified local variables

Sample code:

static

Sample code:

static access features:

Sample code:

The purpose of static keyword

Sample code:

static modified constant

Sample code:


1. final (final state)

2. static

final modifies our member methods, member variables, and classes

Features:

  • Modified variable: The value of this variable cannot be modified and must be initialized (manual assignment)
  • Modified method: This method cannot be overridden
  • Modified class: This class cannot be inherited

Sample code:

Parent.java

package com.Object-oriented.Demo15;

public class Parent {
    private int age; //default
    private final int age2 = 10; //final--value must be initialized

    /**
     * Classes, member properties, and member methods modified by the final keyword
     * 1. Classes modified with the final keyword cannot be inherited
     * 2. Member methods modified by the final keyword cannot be overridden
     * 3. The value of the member attribute modified by the final keyword (the value must be initialized) cannot be modified.
     */
// public final void a1() { //Error report, final--cannot be overridden
// }
    public void a1() {
// this.age2= 20; // Error, final - cannot be modified
    }
}

Student.java

package com.Object-oriented.Demo15;

public class Student extends Parent {
    @Override
    public void a1() {

    }
}

final modified local variable

  • Basic data type: The value of the basic data type modified by final cannot be modified;
  • Reference data type: final modified reference data type. The reference type address cannot change, but the member attribute values in the reference type address can change.

Example code:

Student.java

package com.Object-oriented.Demo16;

public class Student {
    public int age = 30;
}

StudentDemo.java

package com.Object-oriented.Demo16;

public class StudentDemo {
    public static void main(String[] args) {
        // Use the final keyword to modify local variables
        final int age = 20;
// age=30; //Error reported, local variables modified with final are basic data types and cannot be modified.
       final Student student = new Student(); //Reference type
        student.age = 10; // Use final to modify the local variable. It is a reference type. The address of the reference type cannot be modified.
        // However, the attribute value in the address of the reference type can be modified.
// student = null; // Error, the address of the reference type cannot be modified
// student = new Student(); // Error, the address of the reference type cannot be modified
    }
}

static

  • static (static), can modify member methods and member variables
  • Characteristics of static modification: shared access by all objects of the class.

Example code:

Student.java

package com.Object-oriented.Demo17;

public class Student {
    public String name;
// public String school = "Tsinghua University"; //Method 1 - member variables
    public static String school; //Method 2 - static variable

    /**
     * Member methods modified by the static keyword - static methods
     * Member variables modified by the static keyword - static variables
     */

    public void show() { //Member method
        System.out.println(name + "," + school);
    }

}

Test01.java

package com.Object-oriented.Demo17;

public class Test01 {
    public static void main(String[] args) {
        /**
         * Question why the s1 object does not have a static variable school prompt?
         *Answer: The method characteristics of static variables: directly through the class name.
         * Student.school="Tsinghua University"
         * The following s1 s2 s3.school are not required
         */
        Student.school="Tsinghua University";
        // All objects of the class modified by the static keyword share access
        Student s1 = new Student();
        Student s2 = new Student();
        Student s3 = new Student();
        s1.name = "Xiao Ming";
// s1.school = "Tsinghua Tsinghua";
// s1.school = "Tsinghua University"; // S1 here does not have a school object, but no error is reported when writing a class.
        // s2.school does not need to be assigned a value. If it is not static, s2.school will be null if it is not assigned a value.
        s1.show();

        s2.name = "Xiaojun";
// s2.school = "Tsinghua University";
        s2.show();
        s2.school = "Peking University";
        s3.show(); //s3.school here is Peking University
        /**
         * s1.school = "Tsinghua Tsinghua";
         * s2.school = "Tsinghua University";
         * Redundant
         * Method 1. The form of the default value optimizes the code. The s1 and s2 objects do not need to be assigned to school.
         * Method 2. Static variable static
         *
         */
    }
}

static access features:

Non-static member methods

  • Ability to access static member variables
  • Ability to access non-static member variables
  • Ability to access static member methods
  • Ability to access non-static member methods

Static member method

  • Can access static member variables, but cannot directly access member variables
  • Can access static member methods, but cannot directly access member methods

Example code:

Student.java

package com.Object-oriented.Demo18;

public class Student {
    /**
     * Member properties and methods
     */
    private int age; //non-static
    public static int staticAge;//static

    // non-static
    public void a1() {
// In the a1 non-static method, you can access non-static methods, static methods, static member attributes, and non-static member attributes.
        System.out.println(age); //Access non-static member properties
        System.out.println(staticAge); //Access static member properties
        staticA2(); //Access static method
        staticA3();
        a4(); //Access in non-static method
    }

    // static
    public static void staticA2() {
        // In staticA2 static method, access non-static method static method static member attribute non-static member attribute
        /**
         * Among staticA2 static methods, what are the characteristics of non-static methods and static methods?
         * In staticA2 static method, non-static member methods cannot be accessed, but static methods can be accessed.
         */
// a1(); //Error is reported. In static methods, non-static member methods cannot be accessed. If you want to access, you only need to change a1() to static.
        staticA3(); // Static can access static
    }
    public static void staticA3() {
        /**
         *staticA3 In a static method, non-static member properties cannot be accessed, but static member properties can be accessed.
         */
// System.out.println(age); //Error reported, non-static member properties cannot be accessed
        System.out.println(staticAge); //Can access static member properties
    }
    //non-static
    public void a4(){

    }
}

Purpose of static keyword

  • One sentence description is: it is convenient to call (method/variable) without creating an object.
  • Obviously, methods or variables modified by the static keyword do not need to rely on objects for access. As long as the class is loaded, it can be accessed through the class name.
  • static can be used to modify class member methods and class member variables. In addition, static code blocks can also be written to optimize program performance.

Example code:

Test01.java

package com.Object-oriented.Demo18;

public class Test01 {
    public static void main(String[] args) {
        /**
         * static application scenario: If you do not want to access member methods and properties through new
         * static modifies member methods and member properties into static member properties and static member methods
         */
        Student s = new Student();
        s.a1();
// s.staticA2();//s does not have the object staticA2, but it does not report an error. It is not officially recommended.
        Student.staticA2();
        /**
         * shared by all objects in the class through static
         * Class name.Static method
         *
         * Summary: If it is a member method and attribute modified by static
         * We can access (member methods and properties) directly through "class name."
         *
         * Instance access member methods and member properties
         * Non-static member methods and member properties must pass new the object "instance." Member methods and member properties
         */
    }
}

static modified constant

Application scenarios of the static keyword, constants in java (Constant)

Sample code:

Constant.java

package com.Object-oriented.Demo19;

public class Constant {
    /**
     * The definitions of this class are all constants
     * What is a constant?
     * 1. After defined, the value cannot be modified (final)
     * 2. Constant names are all in uppercase letters
     * 2.1 It is too troublesome to write in uppercase. First write in lowercase, and then use the shortcut keys to change to uppercase.
     * 2.2 Shortcut keys in IDEA (mouse cursor (constant name) Ctrl + Shift + u), press it again to change to lowercase
     */
    public static final int CODE_200 = 200;
    public static final int CODE_404 = 404;
    public static final int CODE_500 = 500;
    /**
     *JavaWeb development
     * CODE_200 means success
     * CODE_404 means not found
     *CODE_500 means error
     */
}

Test01.java

package com.Object-oriented.Demo19;

public class Test01 {
    public static void main(String[] args) {
// Constant constant = new Constant(); // Directly use "class name." to access later.
        int code = 200;
        if (Constant.CODE_200 == code) { // JavaWeb development time
            System.out.println("success");
            return;
        }
        if (Constant.CODE_404 == code) {
            System.out.println("Not Found");
            return;
        }
        if (Constant.CODE_500 == code) {
            System.out.println("Error");
            return;
        }

    }
}

Next article: In-depth understanding of main method

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java skill treeVariables and constants in JavaConstants in Java 135474 people are learning the system