Final and finally, how much do you know?

Source: Internet
Author: User
Tags finally block

One: final type modifier in Java

(0)finally although in English grammar is final adv. form, the two are very similar, but the Java application environment is quite different.

(1) 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

This class is no longer extended, so it is designed as the final class. The final method cannot be overridden by a method of a class, but can be inherited.

(2)final method

If a class does not allow its subclasses to overwrite a method, you can declare this method as the final method. There are two reasons for using the final method:

First, lock the method to prevent 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.

(3) Final variable (constant)

A constant is represented by a final modified member variable, which can only be assigned once and the value cannot be changed after the assignment!

There are three final modified variables: static variables (statically belongs to the class), instance variables (which belong to class objects), and local variables, representing three types of constants, respectively.

As you can see from the example below, once the initial value is given to the final variable, it cannot be changed.
In addition, 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.

(4) 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.
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 cannot be used to modify a construction method.
Example:

public void F1 (final int i) {}//i is of the final type, and the value is not allowed to change.

In short, a word in the description, learn C + + friends, will know very similar to C + + show off the const type modifier

Second: The finally statement block in Java

(1) applied in try ^ Catch ^ finally exception handling

Finally provides a finally block to perform all cleanup operations when the exception is handled. Mainly used in conjunction with try-catch-finally.

(2) The function of the finally statement block

No matter what happens, the statements in the finally block are bound to be executed and executed before the return.

According to the Java specification, if return is in try-finally or catch-finally, then 2 return

is executed and eventually returned to the caller there is a return in finally, and if there is no return in finally the return is returned in the catch, but the code in finally is also executed.

(3) Finally although in English grammar is final adv. form, the two are very similar, but the Java application environment is quite different.

(4) Try ^ Catch ^ Finally exception handling detailed click

(5) One may also ask, I often see the following form:

public void static main (string[] args) {}, which is a static method of a class and is also the main function entry for a project

Private final static String Myurl = "www.baidu.com/"; This is similar to global constants for declared classes

Three: Overview

(1) Although it exists in Java, it does not have too many associations: keywords in Final:java, modifiers.

1. If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared at the same time as the Absrtact abstract class and the final class.
2. If you declare variables or methods as final, you can guarantee that they will not be changed in use.
2.1 A variable declared as final must be given an initial value at the time of declaration, and can only be read in subsequent references and cannot be modified.
2.2 The method declared final can only be used and cannot be overloaded.

(2) An exception handling mechanism of Finally:java.             

Finally is the best complement to the Java exception handling model. The finally structure makes the code always executes, regardless of whether or not an exception occurs. Use finally to maintain the internal state of an object and to clean up non-memory resources. Especially in the case of shutting down the database connection, if the programmer puts the close () method of the database connection in the Finally, it will greatly reduce the chance of program error.

Final and finally, how much do you know?

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.