Final
1. Retouching data
The declaration data is a constant, either a compile-time or a constant that cannot be changed after the runtime is initialized.
- For basic types,final causes the values to be unchanged;
- For reference types,final makes the reference immutable, that is, you cannot reference other objects, but the objects to which they are referenced can be modified.
Final int x = 1= 2; // cannot assign value to final variable ' x ' Final New = 1;
2. Modification Methods
Declarative methods cannot be overridden by a quilt class and cannot coexist with abstract .
Note: Theprivate method is implicitly specified as final, and if the method defined in the subclass is the same as a private method signature in the base class, the method of the subclass is not to overwrite the base class method, but overloaded .
3. Modifier class
Declaring a class is not allowed to be inherited and cannot coexist with abstract .
Static
1. Static variables
Static variables have only one copy in memory and are initialized once for the first time the class is used .
- Static variables: All instances of the class share static variables, which can be accessed directly through the class name;
- Instance variables: Each time an instance is created, an instance variable is generated, which is die with the instance.
Public class A { privateint x; // instance variable Public Static int y; // Static Variables Public Static void Main (string[] args) { System.out.println (A.Y); New A (); System.out.println (a.x); }}
2. Static methods
static methods exist when the class is loaded, and it does not depend on any instances, so The static method must be implemented, that is, it cannotbe an abstract method.
A static method can call a static variable, not an instance variable of the class, because the static method appears as the class loads, and the instance variable depends on the concrete instance.
Static void run () { System.out.println ("Hello");}
3. Static code block
Static code blocks are loaded with multiple static blocks of code as the class loads, and a block of code is loaded in the order of code blocks, one at a time.
Static { System.out.println ("hehe");}
4. Sequence of initialization
static data >> common code blocks, instance variables >> constructors
PS: Static variables and static statement blocks which run first depend on their order in the code.
In the case of inheritance, the order of initialization:
- Parent class (static variable, static statement block block)
- Subclass (static variable, static statement block)
- Parent class (instance variable, normal statement block)
- Parent class (constructor)
- Subclass (instance variable, normal statement block)
- Sub-class (constructor)
Written question: What is the output of the following code? (about initialization and assignment of static variables)
Public classSingleTon {Private StaticSingleTon SingleTon =NewSingleTon (); Public Static intCount1; Public Static intCount2 = 0; PrivateSingleTon () {count1++; Count2++; } Public StaticSingleTon Getsingleton () {returnSingleTon; } Public Static voidMain (string[] args) {SingleTon SingleTon=Singleton.getsingleton (); System.out.println (SINGLETON.COUNT1); System.out.println (Singleton.count2); }}
The answer is: 1 0
Resolution: 1. SingleTon SingleTon = Singleton.getsingleton (); Called the class method, triggering the initialization of the class;
2. When class loads, the JVM allocates memory for static variables of the class and assigns default values: SingleTon = null; count1 = 0; Count2 = 0;
3. Class initialization, the JVM assigns a value to a static variable and executes a static code block, when the class constructor is triggered when assigning a value of new SingleTon () to SingleTon, count1 = 1,count2 = 1 After the constructor method of the class is called;
4.JVM continues to assign values to Count1 and Count2, Count1 no assignment operation, count2 = 0;
Summary: The Declaration and assignment of a static variable are separate
Java Basics-Keyword final, static