In Java, the final keyword is used to declare that the object has immutable. The objects here include variables, methods, and classes, which are similar to the const keyword in C++.
immutable means that after the object is created, the state cannot be changed. You can consider using the final keyword from three perspectives:
final variable
Define final Object a, then a can only be initialized once. Once initialized, the data of a cannot be modified. If a is a reference type, other objects cannot be rebinded.
The final variable that is not initialized is called blank final. If it is a member variable, it must be initialized or assigned in the constructor.
example:
class Circle { static final double PI = 3.1415926; final int radius = 5; final int xPos; final int yPos; public Circle(int x, int y) { xPos = x; yPos = y; }} Final method
If you define the final method, the method cannot be overloaded. The method designer does not want other related functions to occur due to overloading the method.
example:
class BaseClass { public final void method() {}}class DerivedClass extends BaseClass { public final void method() {} // Compilation error}It should be noted that the definition of final method may not necessarily produce the effect of inline, because whether the method is inline depends on the JVM's strategy, rather than the final keyword, it is inaccurate to improve the efficiency of the method through final design.
final class
Final class X defined by final class X cannot be inherited.
In Java, the String class is designed as final, and its definition is as follows
The code copy is as follows:
public class final String extends Object implements Serializable, Comparable<String>, CharSequence
Internal class and final
When defining anonymous inner class in a method, the inner class can only access final type variables in the method, so that the Java compiler can capture the value of the variable in advance and save a copy in the inner class. When the method is destroyed, the memory space of the inner class is still complete.
example:
public class Wrapper { public static void main(String[] args) { // Object obj = null; // Compile error final Object obj = null; new Thread(new Runnable() { public void run() { obj = "hello"; } }).start(); }} PS: Issue that internal anonymous class cannot access external non-final variables
This sounds a bit difficult to talk about, but in fact, I want to talk more about some features of Java internal classes.
The reason I think of this question is that when I was reading the code about HTTP keepalive in the JDK source code recently, one of the source files, sun.net.www.protocol.http.HttpURLConnection.java, accidentally saw the following code.
final boolean result[] = {false};java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() { public Object run() { try { InetAddress a1 = InetAddress.getByName(h1); InetAddress a2 = InetAddress.getByName(h2); result[0] = a1.equals(a2); } catch (UnknownHostException e) { } catch (SecurityException e) { } return null; }});return result[0];Java's anonymous internal classes cannot access non-final variables of the corresponding function. To access external local variable, this variable must be defined as fianl first. However, once it is defined as final, the value of this variable cannot be modified in an anonymous internal class, so it is not so easy to return some useful values in an anonymous internal class. This code uses a very clever method, using arrays here to bypass this limitation. Although we cannot modify the reference to the variable result, we can modify the content of the array pointed to by result.
I just want to record a little trick to modify external variables by internal anonymous classes. But since you have arrived here, you might as well continue to see what features or limitations the internal classes have.
Before continuing this article, I feel it is very necessary to clarify some of the Java terms involved in this article. These terms are not easy to translate into Chinese, so we will still describe them in English.
// This is classpublic class JavaTerm { // field or member variable private int field; // constructor public JavaTerm() { } // method public void method() { // local variable int localVariable = 0; // local class class LocalClass { public LocalClass() { } } // anonymous class new Runnable() { public void run() { } }; }}Today we will focus more on local class and anonymous class, both of which belong to inner class.
Java allows us to define another class in a class, called nested class, and nested class can be divided into two categories, one is static nested class, and the other is non-static nested class, also known as inner class. inner class can also be divided into local class and anonymous class.
Some limitations of anonymous class