Home > Java > javaTutorial > body text

Detailed explanation of the differences between Java modifiers abstract, static, and final

高洛峰
Release: 2017-01-24 15:42:10
Original
1527 people have browsed it

static means static, which can modify properties, methods and code blocks.

1.static modifies the attribute (class variable), then this attribute can be accessed using class name.property name, which means that this attribute becomes a class variable of this class and is shared by objects of this class. This attribute is public to all classes. (Shared class variables have nothing to do with objects, only classes).

During the class loading process, the class itself is also saved in the file (the bytecode file stores the class information). Java will read the class file (bytecode file) through the I/O stream. JVM (java virtual machine), this process becomes class loading. JVM (java virtual machine) will find bytecode files through the class path (CLASSPATH).

Class variables will be automatically initialized when loading. The initialization rules are the same as instance variables.

Note: The instance variables in the class are initialized when the object is created. The attributes modified by static, that is, the class variables, are created and initialized when the class is loaded. The process of class loading is once. That is, class variables will only be created once.

2.static modified method (static method) will make this method public to the entire class and can be accessed using class name.method name.

Note: static modified methods cannot directly access non-static (static) members (including methods and properties) of this class. Non-static (static) methods of this class can access static members of this class. (including methods and properties), static methods can be called. Static methods should be used with caution. This keyword cannot appear in static methods.

Note: Static methods in the parent class cannot be overridden to non-static methods in the subclass. Under the premise of complying with the coverage rules, in the parent and subclasses, the static methods in the parent class can be static methods in the subclass. Method overriding, but no polymorphism. (When using an object to call a static method, you are actually calling a static method of the compile-time type)

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

The main method in java must be written as static because the object cannot be created when the class is loaded. Because static methods can be called without objects, they are in the main method of the class. When the class is loaded, the program can be run through the main method entry.

3.static modifies the initial code block. At this time, this initial code block is called a static initial code block. This code block is only executed once when the class is loaded. You can initialize a class with a static initialization block.

Dynamic initial code block, written in the "{}" in the class body, this code block is run when the initialization properties of the generated object are generated. This type of code block is called a dynamic initialization code block.

When will the class be loaded? The class will be loaded when an object is created. The class will also be loaded when calling static methods in the class or accessing static properties. When loading a subclass, the parent class must be loaded first. Class loading has a lazy loading principle, and it will only be loaded when it must be loaded.

Final modifier can modify variables, methods, and classes

1.final modified variables
The variables modified by fianl will become constants (constants should be capitalized). Once assigned, they cannot Change, (you can assign a value directly during initialization, or you can assign a value in the constructor. You can only choose one of these two methods, and you must assign a value to the constant). The constants of fianl will not have a default initial value. For The final modifier is often used together with the static modifier when assigning values ​​directly during initialization.

2. Final modified method. The method modified by final will not be overridden by its subclasses, and the stability of the method cannot be overridden.
3. Final modified class, the class modified by final will not be inherited. The methods in final classes are also final.

Note: final cannot be used to modify the constructor. If there is a constant attribute in the parent class, the class loading of the parent class will not be performed when the constant attribute is used in the subclass.

Invariant mode, the properties of the object will not change once it is created. Use final to modify properties, also use final to modify classes (strong immutability mode), and use final to modify properties (weak immutability mode).

Typical embodiment of immutable mode: java.lang.String class, immutable mode can realize object sharing (one object instance can be assigned to multiple object variables)

Pooled The idea is to put the data that needs to be shared in the pool (save space, share data)
Only the String class can create objects using literal values ​​in "". In the String class, when it is created with a literal value, it will be searched in the string pool space of the Java method space. If there is one, the address of the string in the string pool will be returned, and this address will be paid to the object variable. If not, a string object will be created in the string pool and its address will be returned to the purchase object variable. When another object is created with a literal value, the above process will be repeated.
If new creates an object of String class in the heap space, the above process will not occur.

The intern() method in the String class will compare the string in the String class object created in the heap space with the string pool. If there is the same string, return the string to the string pool. the address of.

The immutable mode is quite troublesome when modifying and adding operations to objects, and it will generate a lot of intermediate garbage objects. The overhead of creating and destroying resources is considerable.

The String class is very inefficient when connecting strings because the properties of the objects it generates cannot be modified. When connecting strings, only new objects can be created.

For many string connections, the StringBuffer class should be used. When using objects of this class to perform string connections, no redundant intermediate objects will be generated, thereby optimizing efficiency.

Abstract (abstract) modifier can modify classes and methods
1.Abstract modifier will make this class an abstract class. This class will not be able to generate object instances, but it can be used as object variables. The declared type, that is, the compile-time type, the abstract class is like a semi-finished product of a class, and subclasses need to inherit and override the abstract methods in it.

2. The abstract modification method will make this method an abstract method, that is, it only has a declaration (definition) but no implementation. The implementation part is replaced by ";". Requires subclasses to inherit the implementation (override).

Note: A class with abstract methods must be an abstract class. However, abstract methods are not necessarily all abstract methods, they can also be concrete methods.
abstract modifier must be placed before the class name when modifying the class.

Abstract modification method requires its subclasses to override (implement) this method. When calling, you can call the method covered (implemented) by the subclass in a polymorphic manner, which means that the abstract method must be implemented in its subclass, unless the subclass itself is also an abstract class.

Note: The parent class is an abstract class with abstract methods. Then the subclass inherits the parent class and implements (overrides) all the abstract methods in the parent class. Only then can the subclass create instances of objects. capabilities, otherwise the subclass must also be an abstract class. An abstract class can have a constructor method, which is the constructor method of the parent class (abstract class) that the subclass needs to call when constructing the subclass object.

Final and abstract, private and abstract, static and abstract, these are modifiers that cannot be put together, because the method modified by abstract must be implemented (covered) in its subclass in order to be polymorphic Call, the above modifiers cannot be overridden by subclasses when modifying the method. Final cannot be overridden. Private cannot be inherited to subclasses, so it cannot be overridden. Static can be overridden, but it will be called when calling. For compile-time type methods, because the methods of the parent class are called, and the methods of the parent class are abstract methods and cannot be called, the modifiers on them cannot be put together.

Abstract (abstract) method represents a certain standard, defines the standard, defines the function, and implements the function in the subclass (the subclass inherits the parent class and needs to provide the implementation of the abstract method inherited from the parent class ).
The method cannot be realized for a while, or it is intended to be implemented by subclasses and defines a certain standard. This method can be defined as abstract. (abstract)

Template method pattern
Use abstract to separate the formulation of standards and the implementation of standards. The formulation of standards is the template, and implementation is implemented according to the template standards, that is, inheriting the template and realizing the corresponding functions in the template method. Methods that are not allowed to be modified in the template can be modified with fianl. This method cannot make abstract methods. To ensure safety and encapsulation, the non-public parts of the template should be modified with protected.

For more detailed explanations of the differences between Java modifiers abstract, static, and final, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!