Java Review Note 6--final&static

Source: Internet
Author: User

Final & Static

The final class cannot be inherited, there are no subclasses, and the methods in the final class are final by default. The final method cannot be overridden by a method of a class, but can be inherited. The final member variable represents a constant, which can only be assigned once, and the value will no longer change after the value is assigned. final cannot be used to modify a construction method. Note: The private member method of the parent class is not overridden by the class method, so the private type method defaults to the final type.

Final Class

The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. In the design class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is certain that the class will not be extended, then it is designed as the final class.

Final Method

If a class does not allow its subclasses to overwrite a method, you can declare this method as the final method. The final method is used for two reasons: first, locking the method, preventing any inherited classes from modifying its meaning and implementation. second, efficient. The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.

Final Variable

When the final variable is defined, it can be declared without giving the initial value, which is also called the final blank, and in any case the compiler ensures that the blank final must be initialized before it is used. However, final whitespace provides greater flexibility in the use of the final keyword final, so that the final data members in a class can be implemented differently depending on the object , but with the same characteristics that remain constant.

Final Parameter

When the function parameter is the final type, you can read it using the parameter, but you cannot change the value of the parameter.

void MSS (final int i) {

? i++;//Error

}

Static

member variables and member methods that are modified by static are independent of any object of the class. That is, it does not depend on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.

Static member variables and member methods that are decorated with public are essentially global variables and global methods, and when you declare objects of its class, you do not generate a copy of the static variable, but all instances of the class share the same static variable.

The static variable can have a private adornment before it, indicating that the variable can be used in the static code block of the class, or in other static member methods of the class (which can also be used in non-static member methods-nonsense), but it is important that the class name cannot be referenced directly in other classes. In fact, you need to understand that private is the access permission limit, static means do not instantiate can be used, so it is easier to understand more. Static plus other access keyword effects and so on.

static variable

Static variables have only one copy in memory, and the JVM allocates only one memory

Instance variables allocate memory once for each instance generated

static MethodStatic code block

A static block of code is also called a code block, which is an independent block of static blocks in a class that is separate from a class member, can have more than one position, it is not in any method body, and the JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, The JVM executes them sequentially in the order in which they appear in the class, and each code block is executed only once

public class test{

? private int A;

? static {

? ....

? }

}

A static Main method so that the JVM can be called directly without creating an instance while running the main method.

Static and abstract

It's not a logical contradiction, it's a language shortcoming, multiple other languages the this notion. "Abstract" mean "implemented in subclasses", "Static" means "executed on the class rather than class instances" there are N o Logical Contradiction

static and final use together

For instance constants that are static and final modified, the instance itself can no longer be changed, but for some instance variables of the container type (for example, ArrayList, HashMap), the container variable itself cannot be changed, but the object stored in the container can be modified, which is a lot of programming.

Java Review Note 6--final&static

Related Article

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.