Java non access modifier

In addition to the access modifiers, we have non access modifiers. This mainly describes the static and final modifiers. Other modifiers will be used later.

I. static modifier

static modifiers are used to modify methods and variables.

1. Create StaticTest.java

public class StaticTest {
    private static int num = 0;////Static variables (class variables) cannot be used in a class's non static methods.
    public static int getNum() {
        //Static int num = 2; / / static variable is class variable, local variable cannot be declared as static variable
        return num;
    }

    public static void add() { //Static methods cannot use non static variables of a class.
        num++;
    }

    public static void main(String[] args) {
        System.out.println("num:" + StaticTest.getNum() + " start");
        for (int i = 0; i < 10; ++i){                                                                                                                                                                        
            add();//Do not belong to any object, even if no object is created, variables and call methods can be used
        }
        System.out.println("num:" + StaticTest.getNum() + " end");
    }
}

The static usage here is different from that of c. only by observing the minutiae can we understand java better.

2, operation

javac StaticTest.java
java StaticTest

The results are as follows:

II. final modifier

1. Create FinalTest.java

public class FinalTest {
    final int value = 10; //Instance variables
    final int value2;

    public FinalTest() {
        value2 = 10;//No error will be reported. You can directly assign values in the construction method.
        System.out.println("value2=: " + value2);
    }
    public final void test() {
        int value = 9;//Local variable: no error will be reported because it is reassigned to int type.                                                                                                                                        
        //value = 9; / / an error is reported. Variables defined by final cannot be reassigned.
        //value2 = 8; / / an error is reported and cannot be assigned in the method
        System.out.println("value=: " + value);
    }
    public static void main(String []args) {
        FinalTest getText = new FinalTest();
        getText.test();
    }
} 

The main focus here is on final variables and methods. The final variable cannot be copied again, while the final method cannot be overridden.

2, operation

javac FinalTest.java
java FinalTest 

The results are as follows:

3. Precautions

  • When final modifies a class, it means that the class cannot be inherited.
  • The private method of a class is implicitly specified as the final method. Methods decorated with final cannot be overridden (including subclasses).
  • A variable defined by final must have an initial value (it cannot be assigned in a method) and can only be initialized once.

Tags: Java

Posted on Wed, 04 Dec 2019 07:58:55 -0800 by olaf