In Java, final, finally, and finalize

Source: Internet
Author: User

One final

Final can be used to modify:

1. Variables

If the basic type is modified, it indicates a constant.

If the reference type is modified, it indicates that the referenced variable is immutable, but the object it points to is variable.

If the parameter of the modifier method is used, the parameter cannot be assigned a value within the method body.

There is also a more appropriate saying that if a variable or method parameter is modified by final, it means that it can only be assigned once, but the Java Virtual Machine does not remember to assign values once when setting the default value for the variable.

Variables modified by final must be initialized in the following ways:

Initialized during definition;

Final modified variables can be initialized in the initialization block or in the method constructor, but cannot be initialized in the static initialization speed;

Static variables modified by final can be initialized in the static initialization block, but cannot be initialized in the initialization block.
Here, by the way, the static {} statement block is executed only once when the class is loaded, and the {} statement block is executed every time the object is instantiated.

2. Method

The final modification method cannot be overwritten, but can be continued.

Methods With Private access permissions can also be added with final modification. However, because the subclass cannot continue with the private method, it cannot be rewritten. The compiler treats the private method according to the final method, which improves the efficiency of the method when it is called. However, subclass can still define methods with the same structure as the private method in the parent class, but this does not produce the effect of rewriting, and there is no necessary link between them.

3. Class

The most common string class is final. Since the final class does not promise to be continued, the compiler treats all its methods as final during processing, so the final class has a higher efficiency than the ordinary class. Abstract classes defined by the keyword abstract contain abstract methods that must be implemented by the subclasses that continue to be self-defined. Therefore, the same class cannot be modified by both final and abstract classes. Similarly, final cannot be used to modify interfaces. All methods of the final class cannot be overwritten, but this does not mean that the attribute (variable) value of the final class cannot be changed. To ensure that the attribute value of the final class cannot be changed, you must add final modification to it.

II finally

It can only be used in a try/catch statement with a block, indicating that the statement is always executed.

Public final class finallytest {public static void main (string [] ARGs) {try {Throw new nullpointerexception ();} catch (nullpointerexception e) {system. out. println ("program throws an exception");} finally {system. out. println ("executed finally statement block ");}}}

The running result illustrates the role of finally:

1. The program throws an exception.

2. The finally statement block is executed.

Note: return, continue, and break cannot prevent the execution of finally statements. Because every try block can cause exit statements, a specified bytecode is associated with finally at the bytecode level ., therefore, finally will be executed. But system. Exit (0) can.

3 finalize ()

Because finalize () belongs to the object class, all classes have this method, and any subclass of the object can override (override) This method, in this example, you can release system resources or perform other cleanup operations, such as closing input and output streams.

 

 

Address: http://www.csdnjava.com/thread-42983-1-1.html

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.