Home  >  Article  >  Java  >  What is the use of final keyword in Java?

What is the use of final keyword in Java?

怪我咯
怪我咯Original
2017-06-23 11:48:041513browse

The final keyword in Java is very important, it can be applied to classes, methods and variables. In this article I will take you to see what is the final keyword? What does it mean to declare variables, methods and classes as final? What are the benefits of using final? Finally, there are some examples of using the final keyword. final is often used together with static to declare constants. You will also see how final can improve application performance.

What is the meaning of the final keyword?

Final is a reserved keyword in Java that can declare member variables, methods, classes and local variables. Once you make the reference declaration final, you cannot change the reference. The compiler will check the code and if you try to initialize the variable again, the compiler will report a compilation error.

What is a final variable?

Any member variables or local variables (variables in methods or code blocks are called local variables) that are declared final are called final variables. Final variables are often used with the static keyword as constants. The following is an example of a final variable:

public static final String LOAN = "loan";
LOAN = new String("loan") //invalid compilation error

Final variables are read-only.

What is a final method?

Final can also declare methods. Adding the final keyword in front of a method means that this method cannot be overridden by methods of subclasses. If you think the function of a method is complete enough and does not need to be changed in subclasses, you can declare the method as final. Final methods are faster than non-final methods because they are statically bound at compile time and do not need to be dynamically bound at runtime. The following is an example of a final method:

class PersonalLoan{
    public final String getName(){
        return "personal loan";
    }
}
 
class CheapPersonalLoan extends PersonalLoan{
    @Override
    public final String getName(){
        return "cheap personal loan"; //compilation error: overridden method is final
    }
}

What is a final class?

A class modified with final is called a final class. Final classes are usually functionally complete and they cannot be inherited. There are many classes in Java that are final, such as String, Interger and other wrapper classes. The following is an example of the final class:

final class PersonalLoan{
 
    }
 
    class CheapPersonalLoan extends PersonalLoan{  //compilation error: cannot inherit from final class
 
}

The benefits of the final keyword

The following summarizes some of the benefits of using the final keyword

The final keyword improves performance. Both JVM and Java applications cache final variables.

Final variables can be safely shared in a multi-threaded environment without additional synchronization overhead.

Using the final keyword, the JVM will optimize methods, variables and classes.

Immutable class

To create an immutable class, use the final keyword. An immutable class means that its objects cannot be changed once they are created. String is a representative of immutable classes. Immutable classes have many benefits. For example, their objects are read-only and can be safely shared in a multi-threaded environment without additional synchronization overhead.

The above is the detailed content of What is the use of final keyword in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn