java learning--static and final

static is used with variables and methods to indicate "static".

1 Static variables belong to a class, not to any independent object, so you can access static variables without creating an instance of the class.

2 A static method is a method that cannot perform operations on an object. Because static methods cannot manipulate objects, instance variables cannot be accessed in static methods, but only static variables of their own class.

3 A static method of a class can only access static variables;

4 A static method of a class cannot directly call a non-static method;

5 If the access control permission allows, static variables and static methods can also be accessed through objects, but it is not recommended;

6 The current object does not exist in the static method, so this cannot be used, and of course super cannot be used;

7 Static methods cannot be overridden by non-static methods;

8 Constructors are not allowed to be declared static;

9 Local variables cannot be modified with static.

 

Note: As long as the class is loaded, it will be initialized whether you use this static variable or not.

 

public class Demo {
    static int i;
    int j;

    public static void main(String[] args) {
        Demo obj1 = new Demo();
        obj1.i = 10;
        obj1.j = 20;
       
        Demo obj2 = new Demo();
       
        System.out.println("obj1.i=" + obj1.i + ", obj1.j=" + obj1.j);
        System.out.println("obj2.i=" + obj2.i + ", obj2.j=" + obj2.j);
    }
}

 

 

The data modified by final has the characteristics of "final state", which means "final".

1 Final modified classes cannot be inherited.

2 Final modified methods cannot be overridden by subclasses.

3 A final modified variable (member variable or local variable) becomes a constant and can only be assigned a value once.

4 Final modified member variables must be assigned at the same time of declaration. If there is no assignment at the time of declaration, there is only one chance of assignment, and it can only be explicitly assigned in the constructor before it can be used.

5 Final modified local variables can only be declared without assignment, and then a one-time assignment can be performed.

 

Final is generally used to modify data whose general functions, implementation methods or values ​​cannot be changed at will to avoid misuse, such as methods for implementing mathematical trigonometric methods, exponentiation and other functions, and mathematical constants π=3.141593, e =2.71828 etc.

 

public final class Demo{
    public static final int TOTAL_NUMBER = 5;
    public int id;
    public Demo() {
        // Illegal, secondary assignment to final variable TOTAL_NUMBER
        // because ++TOTAL_NUMBER is equivalent to TOTAL_NUMBER=TOTAL_NUMBER+1
        id = ++TOTAL_NUMBER;
    }
    public static void main(String[] args) {
        final Demo t = new Demo();
        final int i = 10;
        final int j;
        j = 20;
        j = 30; // illegal, double assignment to final variable
    }
}

 

 

 

 

 

 

 The examples in this article are referenced from: http://www.weixueyuan.net

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326288922&siteId=291194637