编程知识 cdmana.com

Learn the final and static keywords in Java in one minute

final key word

Concept :final In the end , immutable .final It's a modifier , It can be used to modify classes , Class members , And local variables . Can't modify constructor .

characteristic :

1.final Decorated classes cannot be inherited , But it can be inherited from other classes .

class Yy {}
final class Fu extends Yy{} // Inherit Yy class 
class Zi extends Fu{} // Cannot inherit Fu class 

2.final Decorated methods cannot be overridden by subclasses , But the parent class is not final Modification methods , Subclasses can be rewritten with final.

class Fu {    
    // final The method of decoration , Cannot be overwritten , But it can be inherited 
    public final void method1(){}
    public void method2(){}
}
class Zi extends Fu {
    // rewrite method2 Method 
    public final void method2(){}
}// Join in Java Development and exchange :756584822 Blow water and chat together 

3 final Modified variables are called constants , These variables can only be assigned once . And never change .

final int i = 20;
i = 30; // Error in assignment ,final Modified variables can only be assigned once 

4. The variable value of the reference type is the object address value , The address value cannot be changed , But the value of the object property in the address can be modified .

final Person p = new Person();
Person p2 = new Person();
p = p2; //final Decorated variable p, The recorded address value cannot be changed 
p.name = " Xiao Ming ";// You can change p In the object name Property value 

5. Modifier member variable , You need to assign a value before creating the object , Otherwise, the report will be wrong .( When there is no explicit assignment , Multiple construction methods all need ` For the assignment .)

class Demo {
    // Direct assignment 
    final int m = 100;
    
    //final Modified member variables , You need to assign a value before creating the object , Otherwise, the report will be wrong .
    final int n; 
    public Demo(){
        // In the construction method that you call when you create an object , As a variable n assignment 
        n = 2016;
    }
}

static key word

Concept :static It's a static modifier , It is generally used to modify members of a class . Don't create objects , You can call the method .

characteristic :

1. By static The decorated member variable belongs to the class , An object that does not belong to this class .( in other words , Multiple objects are accessing or modifying static When decorating a member variable , One of the objects will be static Member variable values have been modified , In other objects static The value of the member variable changes , That is, multiple objects share the same static Member variables )

class Demo {
   public static int num = 100;
}

class Test {
   public static void main(String[] args) {
       Demo d1 = new Demo();
       Demo d2 = new Demo();
       d1.num = 200;
       System.out.println(d1.num); // The result is 200
       System.out.println(d2.num); // The result is 200
   }
}

2. By static Modified members can and are recommended to be accessed directly through the class name .

 The format for accessing static members :
 Class name . Static member variable name 
 Class name . Static member method name ( Parameters )
 Object name . Static member variable name          ------ It is not recommended to use , Warning will appear 
 Object name . Static member method name ( Parameters )     ------ It is not recommended to use , Warning will appear 
class Demo {
    // Static member variable 
    public static int num = 100;
    // Static methods 
    public static void method(){
        System.out.println(" Static methods ");
    }
}
class Test {
    public static void main(String[] args) {
        System.out.println(Demo.num);
        Demo.method();
    }
}

matters needing attention

  1. Static content takes precedence over objects , Only static , Out of commission this/super. The content of static modification is stored in the static area .
class Demo {
    // Member variables 
    public int num = 100;
    // Static methods 
    public static void method(){
        //this.num;  Out of commission this/super.
        System.out.println(this.num);
    }// Join in Java Development and exchange :756584822 Blow water and chat together 
}
  1. In the same class , Static members can only access static members
class Demo {
    // Member variables 
    public int num = 100;
    // Static member variable 
    public static int count = 200;
    // Static methods 
    public static void method(){
        //System.out.println(num);  In the static method , Only static member variables or static member methods can be accessed 
        System.out.println(count);
    }
}

3.main Method is a static method, only an entry point for program execution , It doesn't belong to any object , Can be defined in any class .

4. In polymorphic call method , On Compilation = On the left , The parent class has , Compile successfully , The parents don't have , Compile failed

  • function , Static methods , Run static methods in the parent class ,

  • function , Non static methods , Run subclass overriding methods

  • Member variables , Compiling and running are all parent classes

Define static constants

Concept : In development , We want to define a static constant in the class , Usually use public static final Modify the variable to complete the definition . The variable names are all capitalized , Multiple words are underlined .

Define format :public static final data type Variable name = value ;

public class School {
    public static final String SCHOOL_NAME = " Peking University, ";
    public static void method(){
        System.out.println(" A static method ");
    }// Join in Java Development and exchange :756584822 Blow water and chat together 
}

When we want to use static members of a class , No need to create objects , Directly use the class name to access .

System.out.println(Company.SCHOOL_NAME); 
Company.method(); //  Call a static method 

Be careful :

Each member variable in the interface uses by default public static final modification .

All member variables in the interface are already static constants , Because the interface has no constructor , So you have to show the assignment . You can access it directly with the name of the interface .

interface Inter {
    public static final int COUNT = 100;
}

Member variables of the access interface :

Inter.COUNT

 Insert picture description here
image

newest 2020 Organize and collect some high frequency interview questions ( It's all documented ), There are a lot of dry goods , contain mysql,netty,spring, Threads ,spring cloud、jvm、 Source code 、 The algorithm is explained in detail , There's also a detailed learning plan , Interview questions, etc ,
If you need to get this content, please add Q Your appearance :
756584822

版权声明
本文为[osc_ auwxnkoo]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225072637382Q.html

Scroll to Top