The difference between Java modifier abstract,static,final _java

Source: Internet
Author: User
Tags modifier modifiers

Static represents statics, which can modify properties, methods, and code blocks.

1.static modifier attributes (class variables), This property can be accessed using the class name. Property name, which is to make this property a class variable of this class, common to this class object. This attribute is the whole class public. (The common class variables are independent of objects and are related only to classes).

Class is stored in a file (the byte code file holds the class information), Java will read the class's file (bytecode file) into the JVM (the Java Virtual machine) through the I/O stream, which becomes the loading of the class. The JVM (Java Virtual machine) uses the Classpath (CLASSPATH) to find bytecode files.

Class variables are initialized automatically at load time, and the initialization and instance variables are the same.

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

The 2.static Cosmetic Method (static method) makes this method public for the entire class, and can be accessed using the class name. Method Name.

Note:Static-modified methods, which do not directly access non-static (static) members in this class, including methods and properties, can access static members of this class (including methods and properties), and can invoke statics. Static methods should be used with caution. The This keyword cannot appear in a static method.

Note: static methods in the parent class cannot be overridden as non-static methods, and in a parent-child class, a static method in a parent class can be overridden by a static method in a child class, but without polymorphism, if the overriding rule is met. (When using an object to invoke a static method is actually a static method that invokes a compile-time type)

Note: in a parent-child class, a static method can only be overridden by a static method, and a non-static method can only be overridden by a non-static method in a parent-child class.

The main method in Java must be static because the object cannot be created when the class is loaded because the static method can be invoked without the object, so the main method in the class. Where the class is loaded, you can run the program through the main method portal.

3.static modifies the initial code block, at which point the initial code block is called the static initial code block, which is only executed once when the class is loaded. A class can be initialized with a static initial block of code.

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

When the class is loaded, the class is loaded when the object is created, and the class is loaded by calling a static method in the class or accessing a static property. The parent class is loaded first when the subclass is loaded, and the class load has a deferred load rule that is loaded only when it must be loaded.

Final modifier, you can modify variables, methods, classes

1.final modifier variable
Variables modified by fianl become constants (constants should be capitalized), once the assignment cannot be changed, (you can assign a value directly in the initialization, you can also assign a value in the constructor, you can only choose one of the two methods two, you cannot assign a value to a constant), the FIANL constant will not have a default initial value, Used with the final modifier constants and the static modifier directly when initialization is assignment.

2.final Modification Method , the final modified method will not be covered by its subclasses, and the stability of the method cannot be overwritten.
3.final modifier class , the final decorated class will not be inherited. The methods in the final class are also final.

Note:final, can not be used to decorate the constructor method, in the parent class if there is a constant property, the use of the constant property in the subclass is not the parent class load.

Invariant mode, the object will not change once the property is created. With the final modifier attributes, also with the final modifier class (strongly invariant mode), the final modifier attributes (weakly invariant mode).

Typical embodiment of invariant pattern: java.lang.String class, invariant mode can implement object sharing (can be assigned to multiple object variables with one object instance)

Pooling of thought, the need to share data in the pool (save space, share data)
Only the string class can create an object with the literal value in. In a string class, when created with a literal value, it is found in the string pool space of the Java method space, and if so, returns the address of the string in the string pool and pays the address to the object variable. If not, a string object is created in the string pool and the address of the object is returned, and the process is repeated when another object is created with a literal value.
If new is creating an object that creates a string class in heap space, there is no such procedure.

The Intern () method in the string class will match the strings in the string class object created in the heap space with the pairs in the string pool, if the same string returns the address in the string pool of the string.

Invariant mode in the modification of the object, add operation is very troublesome, he will produce a lot of intermediate garbage object. The cost of creating and destroying resources is quite large.

String classes are inefficient when strings are concatenated, because the book nature of the objects that it produces cannot be modified, which means that only new objects can be created when the string is connected.

For many string connections, you should use the StringBuffer class to optimize efficiency by not having redundant intermediate object generation when using objects of this class for string concatenation.

Abstract (abstract) modifier, you can modify classes and methods
the 1.abstract modifier class
makes this class an abstract class that cannot generate object instances, but can be declared as object variables, that is, compile-time types, and abstract classes are like semi-finished products in a class. The abstract method that requires the subclass to inherit and overwrite.

The 2.abstract modification method turns this method into an abstract method, that is, only the declaration (definition) is not implemented, and the implementation part is ";" Replace. A subclass inheritance implementation (overwrite) is required.

Note: classes that have abstract methods must be abstract classes. But abstract classes are not necessarily abstract methods, they can be all concrete methods.
The abstract modifier must be placed in front of the class name when the class is decorated.

The abstract modification method requires that its subclasses overwrite (implement) this method. The method that the subclass overrides (implements) can be invoked in polymorphic mode when invoked, 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, where there are abstract methods, the subclass inherits the parent class, and all the abstract methods in the parent class are implemented (overwritten), the subclass has the ability to create an instance of the object, or the subclass must be an abstract class. An abstract class can have a constructor method, which is the constructor of the parent class (abstract class) that the subclass needs to invoke when it constructs the subclass object.

Final and Abstract,private and abstract,static and abstract, these are modifiers that cannot be put together, because the method of the abstract modification must be implemented (overridden) in its subclasses to be invoked in polymorphic mode, The above modifiers cannot override this method in the cosmetic method period subclasses. Final is not to overwrite, private is not able to inherit to subclass, so also cannot overwrite, static can be overridden, but call the compile-time type of method, because the call is the method of the parent class, The method of the parent class is an abstract method and cannot be invoked, so the modifiers on it cannot be put together.

Abstract methods represent a standard, define criteria, define functionality, and implement functionality in subclasses (subclasses inherit the parent class and need to give an implementation of the abstract method inherited from the parent class).
Method can be defined as abstract by a time when it is not possible to realize how it is implemented, or if it is intended to be implemented by subclasses and to define a standard. (abstract)

Template method Mode
Use abstract to set standards and achieve standards separate, the standard is the template, implementation is according to the template standard to achieve, that is, inherit the template, to implement the corresponding functions in the template method. Methods that are not allowed to be modified in a template can be decorated with fianl, and this method does not allow abstract methods to be used for security, encapsulation, and protected (protection) of parts of the template that are not exposed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.