Java modifiers: abstract static final

  • 2020-04-01 02:20:33
  • OfStack

Static is static. It modifies properties, methods, and code blocks.

1. Static attribute (class variable), Then this property can be accessed with the class name. Property name, that is, the property is a class variable of this class, which is Shared by this class object. This property is class public. Common class variables are not related to objects, but only to classes.

The process of class loading, the class itself is also saved in the file (the bytecode file holds the information of the class), Java will read the file of the class (the bytecode file) into the JVM (Java virtual machine) through the I/O stream, this process becomes the loading of the class. The JVM (Java virtual machine) looks for bytecode files through the CLASSPATH.

Class variables are automatically initialized when loaded, with the same initialization rules as instance variables.

Note: An instance variable in a class is initialized when an object is created. A property that is fixed with static is created and initialized when the class is loaded. That is, class variables are only created once.

2. Static method Will make this method public to the entire class, can be accessed with the class name. Method name.

Note: Static modification of the method, not directly access to the class in the non-static (static) members (including methods and properties), the non-static (static) methods of the class can access the static members of the class (including methods and properties), can call the static methods. Use static methods sparingly. This keyword cannot appear in static methods.

Note: The parent class is a static method, and the subclass cannot be overridden as a non-static method. Under the premise of conforming to the overwriting rule, in the parent class, the static method in the parent class can be overridden by the static method in the subclass, but there is no polymorphism. (calling static methods with objects is actually calling static methods of compile-time type)

Note: In parent-child classes, static methods can only be overridden by static methods, and in parent-child classes, non-static methods can only be overridden by non-static methods.

The main method in Java has to be static because you can't create an object when the class is loaded, because the static method can be called without calling the main method of the class. You can run the program through the main method entry while the class is loaded.

3. Static modifies the initial code block, This initial block of code is called a static initial block of code and is executed only once during class loading. You can initialize a class with a static initialization block.

Dynamic initialization block, written in the body of the class "{}", this block of code is run in the generated object's initialization property is. This block of code is called the dynamic initialization block.

When classes are loaded, classes are loaded when objects are created, and classes are loaded by calling static methods in the class or accessing static properties. When subclasses are loaded, the parent class must be loaded first. Class loading has the principle of lazy loading, and it is loaded only when it must be loaded.

Final modifier, which modifies variables, methods, and classes

1. Final modifies variables
Fianl modified variable will become a constant (constant should be capitalized), once the assignment does not change, (direct assignment during initialization, can also can also be assigned in a constructor, can only be a choice in the two methods, not for constant assignment), the fianl constants do not have a default initial value is assigned to directly in the initialization value final modifier is often used with the static modifier.

2. Final modification method , methods that are modified with final cannot be overridden by their subclasses, and methods that remain stable cannot be overridden.
3. The final class , classes that are final cannot be inherited. Methods in final classes are also final.

Note: Final, cannot be used to modify constructors, in the parent class if there is a constant property, in the child class when the use of constant property is not the parent class class loading.

Immutable pattern, once the object is created the property does not change. Properties are modified with final, classes are modified with final (strongly invariant), and properties are modified with final (weakly invariant).

A typical example of an invariant pattern is the java.lang.string class, which allows objects to be Shared (one object instance can be assigned to multiple object variables)

Pooling the idea of pooling data that needs to be Shared (save space, share data)
Only the String class can create objects with literals in "". In the String class, when created as a literal, it looks in the String pool space of the Java method space, returns the address of the String in the String pool if any, and pays that address to the object variable. If not, a string object is created in the string pool and its address is returned to pay for the object variable, and the process is repeated when another object is created in literal value.
If new creates an object of the String class in the heap space, there is no such procedure.

The intern() method in the String class matches the String in the String class object created in the heap space to the String pool, returning the address in the String pool if there is the same String.

Immutable mode in the object to modify, add operations are quite troublesome, it will produce a lot of intermediate garbage objects. The overhead of creating and destroying resources is considerable.

The String class is inefficient when concatenating strings, because the bookness of the objects it produces cannot be modified, and when concatenating strings it can only create new objects.

For many string concatenations, the StringBuffer class should be used so that there is no extra intermediate object generation when using the class's objects for string concatenation, thus optimizing efficiency.

Abstract modifier that modifies classes and methods
1. Abstract modifies the class,
This will make the class an abstract class, which will not be able to generate object instances, but can be declared as a type of object variable, which is known as a compile-time type.

2. Abstract modification method Will make this method abstract, that is, only declare (definition) without implementation, the implementation part with ";" Instead. Subclasses are required to inherit the implementation (override).

Note: A class with abstract methods must be an abstract class. But an abstract class is not always an abstract method, it can be all concrete methods.
The abstract modifier must come before the class name when it modifies the class.

The abstract modifier simply requires its subclass to override the method. Subclassoverridden (implemented) methods can be called polymorphic when invoked, which means that an abstract method must be implemented in its subclass, unless the subclass itself is an abstract class.

Note: The parent class is an abstract class, in which there are abstract methods, so the subclass inherits the parent class, and all the abstract methods in the parent class are implemented (overwrite), the subclass has the ability to create an instance of the object, otherwise the subclass must be an abstract class. An abstract class can have a constructor that is the constructor of the parent class (the abstract class) that a subclass needs to call when constructing a subclass object.

Final and the abstract, private, and the abstract, the static and abstract, the modifier can't be together, because the abstract modified method is must be realized in its subclasses (cover), to call in polymorphic way, above the modifier in the preparation period in subclasses can override this method, the final is can not cover, private cannot be inherited to the subclass, so also can't cover, the static can be covered, but when the call call compile-time type of the method, Because you are calling a method of a parent class, which is an abstract method and cannot be called, modifiers on cannot be put together.

The abstract method represents some kind of standard, defines the standard, defines the function, implements the function in the subclass (the subclass inherits the parent class and needs to give the implementation of the abstract method inherited from the parent class).
A method can be defined as abstract if it can't be implemented for a moment, or if it is defined as a standard that is intended to be implemented by a subclass. (abstract)

Template method pattern
Abstract is used to separate the formulation standard from the implementation standard, the formulation standard is the template, the implementation is the template standard to achieve, that is, the inheritance of the template, the realization of the corresponding function in the template method. Methods that are not allowed to be modified in the template can be modified with fianl. This method cannot be used to abstract methods. For security, encapsulation, the parts of the template that are not exposed are protected.


Related articles: