Introduction to final and finally use in Java _java

Source: Internet
Author: User
Tags modifier

Final can modify classes, member variables, local variables, and methods.

1.final Modifier member Variable

Initialization of a 1.final member variable

For final-decorated variables, the system does not initialize to 0 by default

Fina Variable Initialization method:

    • Initialized at the time of definition
    • Final variables can be initialized in the initialization block and cannot be initialized in a static initialization block.
    • Static final variables can be initialized in a static initialization block and cannot be initialized in an initialization block.
    • FINA variables can also be initialized in constructors, but static final variables are not available.

2.final Cosmetic Method

When final is used to decorate a method, it means that the method cannot be overridden by a quilt class.

3.final Cosmetic Class

Final-decorated columns are not allowed to be inherited, and the compiler treats all its methods as final, so the final analogy to the general class has higher efficiency. Abstract columns that are defined by the keyword abstract contain abstract methods that must be implemented by inheriting from its subclass, so the same class cannot be decorated with final and abstract at the same time. In the same way, final can not be used to modify interfaces. None of the final class methods can be overridden. This does not mean that the property values of the final class are immutable. To make the final class attribute value immutable, you must add the final modifier to it.

A finally statement can only be used in a try/catch statement and is accompanied by a block of statements, indicating that the statement is always executed at all time.

Copy Code code as follows:

public class Test {
public static void Main (string[] args) {
System.out.println (Returntest ());//false
}
public static Boolean returntest () {
try{
return true;
}finally{
return false;
}
}
}

The value of the expression after the return is calculated, the value is temporarily stored, and then the expression value in finally is calculated and then stored temporarily.
This will overwrite the previously stored values. Finally, go back to the previous return, take the value out of the place where the variable was temporarily stored, and go back. That is the result.

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.