Use of java's final keyword


The content of the article is selected from Shang Silicon Valley, jdk8, eclipse environment

Use of final keyword

The final keyword can modify classes, methods, and variables

final keyword modification class

The classes modified by the final keyword cannot be inherited, such as the String class, System class, StringBuffer class in the Java standard library

package com.atguigu.java3;

public class FinalTest {
    
    

}

//class A extends String{
    
    
//	
//}

Some of the commented compilers will report errors. This is because the String class in the standard library has the keyword final modification, which means that the String class cannot be inherited.

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    
    
    /** The value is used for character storage. */
    private final char value[];

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;

The above is the String definition in the standard library, with keyword final modification.

final modification method

The method modified by the final keyword means that the method cannot be overridden, such as the getClass method in the Object class

//class AA{
    
    
//	public void getClass(){
    
    
//		
//	}
//}

The commented part of the compiler will also report an error, because the getClass method returns the class to which the current object belongs. In the Object class, this method is finalized and cannot be overridden.
ps: Press crtl+shift+t to find the Object class, then press ctrl+o to display all the methods of this class, click getClass.

    public final native Class<?> getClass();

    /**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hash tables such as those provided by

It can be seen that the getClass method in the class also has a final modification
ps: we found that the method here does not display the method body, and there is also a native keyword. The native keyword means that the method body of the method is written in c or c++, not Written in java code, it belongs to the underlying code, so it is not displayed.

final modified variable

  • The final modified variable, the variable at this time becomes a constant, a constant means that it is not allowed to be modified.
  • Variables modified by final are generally written in uppercase.

Final modifies the variable, then final can also modify the attribute, because the attribute also belongs to the variable. When final modifying attributes, it can be explicitly assigned, or initialized in the code block, initialized by the constructor.

Explicit initialization when final modification of attributes

Can not be initialized by default when final modifying attributes

package com.atguigu.java3;

public class FinalTest {
    
    
	final int WIDTH = 0;
}

If there is no final keyword, even if the attribute WIDTH is not assigned an initial value, the default initialization is also 0, but there is a final keyword modification, and explicit initialization must be used.
ps: ctrl+shift+x can change lowercase letters to uppercase letters.

Initialize in the code block when final modifies the attribute

package com.atguigu.java3;

public class FinalTest {
    
    
	final int WIDTH = 0;
	final int LEFT;
	
	{
    
    
		LEFT = 1;
	}
}

Initialization in the constructor when final modification of attributes

Using constructor initialization can assign the value you need to the final attribute of the object, which is more flexible than explicit assignment.

package com.atguigu.java3;

public class FinalTest {
    
    
	final int WIDTH = 0;
	final int LEFT;
	final int RIGHT;
	
	{
    
    
		LEFT = 1;
	}
	
	public FinalTest(){
    
    
		RIGHT =2;
	}
	
//	public FinalTest(){
    
    
//		
//	}
}

The commented out part is reported as an error. When initializing through the constructor, please note that the final attribute must be assigned inside each constructor, because we can only call one when calling the constructor, because we must ensure that each constructor is finalized The attribute assignment.

If you add the RIGHT assignment statement inside the commented out constructor, the compilation will pass.

public class FinalTest {
    
    
	final int WIDTH = 0;
	final int LEFT;
	final int RIGHT;
	
	{
    
    
		LEFT = 1;
	}
	
	public FinalTest(){
    
    
		RIGHT =2;
	}
	
	public FinalTest(int n){
    
    
		RIGHT = n;
	}
}

At this point, you can assign any int value to the attribute RIGHT through the constructor.

ps: Final modified attributes cannot be assigned through objects and attributes, because we assign values ​​through objects and attributes, provided that there is an existing object, and when we create an object, the defined attribute must have an initial value, this The initial value can be the default initial value or the value initialized by the constructor. Anyway, there must be a value. But when we defined the class, we modified the attribute with final, and the attribute became a constant, and because the final modified attribute cannot be initialized by default. So it must be initialized by other means before the object can be created. Therefore, the object. attribute cannot modify the value, because the final attribute is a constant and cannot be modified.

final modifies local variables

public class FinalTest {
    
    
	final int WIDTH = 0;
	final int LEFT;
	final int RIGHT;
	
	{
    
    
		LEFT = 1;
	}
	
	public FinalTest(){
    
    
		RIGHT =2;
	}
	
	public FinalTest(int n){
    
    
		RIGHT = n;
	}
	
	public void show(final int num){
    
    
//		num = 20;//常量不允许被修改,只能被调用
		System.out.println(num);
	}
	
	public static void main(String[] args) {
    
    
		FinalTest test = new FinalTest();
		test.show(10);
	}
	
}

The output result is 10. The
reason why the show method compiles here is that the formal parameter declares the variable in the stack space only when it is called, and passes an actual parameter value, which is similar to the explicit initialization of the attribute. Therefore, the compilation passed. Since the formal parameters have been modified with final, the num in the show method can only be called and cannot be modified. num = 20; The compiler will report an error.

Supplement: static final modified attributes, meaning global constants, static keyword modified attributes means to load through the loading of the class, just this one, through the direct adjustment of the class to reflect the global, final reflects the constant.

Guess you like

Origin blog.csdn.net/Meloneating/article/details/114164412