STATIC, Abstract, Final Model Summary

xiaoxiao2021-03-06  44

Static modifier

Typically, each instance creates a self-instance variable when you create an instance of the class. But in the declaration of the variable, the Static modifier can be used, indicating that the member variable belongs to the class itself, any object generated independently of the class. This member variable is called a static variable (static attribute). The Static modifier can also be used in the declaration of the method, indicating that the method is from the class itself. Only other static member functions can only be called directly in a static member function, or the static properties will be called, otherwise it will cause compilation errors. This or super function cannot be used in a static member function because they are associated with the objects of the class. Static properties and static methods can be used without creating instances. Typical examples are methods and variables in the system class in the java.lang package. We often use system.out.println ("Message"); output information does not create an instance of System because the PrintLn method declares a static method of the System class, which can be used without creating an instance. The following example will help you understand the Static modifier more deeply. Pay attention to the loading order of each code block. Class sample {static int S1 = 3; static int S2; static void display () {system.out.println ("S1 =" S1); System.out.Println ("S2 =" S2);} static { System.out.println ("static block"); S2 = S1 1;} public static void main (StringArgs []) {sample.display ();} Once this class is called, all static variables are initialized. The S1 is assigned to 3, then runs the Static block, which will print a message, and the S2 is assigned to S1 1, i.e., 4. Then the interpreter calls the main member function, which calls the member function Display, the function outputs information of S1 and S2. The results are as follows: C: /> Java Sample Static Block S1 = 3 S2 = 4 It can also be seen from the above example, and a static member function can be called by the class name it belongs.

Abstract modifier

Abstract modifiers indicate that the modified classes are not fully implemented, and they cannot be instantiated. If the Abstract modifier is used in the class's method declaration, the method is an abstract method that requires the subclass implementation. If a class contains an abstract function, this class is also an abstract class that must be used using the Abstract modifier and cannot be instantiated. In the case, the class must be an abstract class: 1. Class contains a clear declared abstract method; 2. Any parent class of the class contains an abstract method that is not implemented; 3. Direct parent interface of the class declares or inheritance A abstract method and this class does not declare or implement the abstract method. As shown in the following example: Abstract Class A1 {Public Int V1; Abstract Void Test ();} Abstract Class A2 Extends A1 {Public Int V2;} Class Sample Extends V2 {Void Test () {}} Because of an abstract method TEST, Class V1 must be declared as an abstract class. Its subclass V2 inherits the abstract method TEST, but does not implement it, so it must also be declared as an abstract class. However, the SAMPLE SAMPLE of V2 is made because it does not have to declare an abstraction. Note: If an instance attempts to create an abstract class will generate compilation errors; if a class is a non-abstract class contains an abstract method, it will generate compilation errors; constructor and static functions and Final modified functions cannot use ABSTRACT modifiers; The interface default is Abstract. Final modifier

If a class is fully implemented and no longer needs to inherit the subclass, it can be declared as a Final class. If the name of the Final class appears behind another class declared Extends sentence, it will generate compilation errors. This indicates that the Final class cannot be any child. Classs cannot be declared at the same time as Abstract and Final, because Abstract methods in the Abstract class will never be implemented. By default, all member functions and instance variables can be overwritten. If you want your variable or member function to be covered by subclasses, you can declare them as Final. For example: final int max_value = 100; indicates that the value of max_value is 100 and cannot be changed. Final variables are a general agreement with uppercase identifiers.

转载请注明原文地址:https://www.9cbs.com/read-118444.html

New Post(0)