Introduction and use of final keyword

* final: final
 * 
 * 1. final can be used to modify the structure: classes, methods, variables
 * 
 * 2. final is used to modify a class: this class cannot be inherited by other classes.
 * For example: String class, System class, StringBuffer class
 * 
 * 3. final is used to modify the method: indicating that this method cannot be overridden
 * For example: getClass();
 * 
 * 4. final is used to modify the variable: this The "variable" at the time is called a constant
 * 4.1 final modification attributes: you can consider assignment positions: explicit initialization, initialization in the code block, initialization in the constructor
 * 4.2 final modification of local variables:
 * especially the use of final modification When it is a formal parameter, it indicates that this formal parameter is a constant. When we call this method, we assign an actual parameter to the constant parameter. Once assigned
 *, this parameter can only be used in the method body, but cannot be reassigned.
 *           
 * static final is used to modify attributes: global constants

public class FinalTest {
	
	final int WIDTH = 0;
	final int LEFT;
	final int RIGHT;
//	final int DOWN;
	
	{
		LEFT = 1;
	}
	
	public FinalTest(){
		RIGHT = 2;
	}
	
	public FinalTest(int n){
		RIGHT = n;
	}
	
//	public void setDown(int down){
//		this.DOWN = down;
//	}
	
	
	public void doWidth(){
//		width = 20;
	}
	
	
	public void show(){
		final int NUM = 10;//常量
//		NUM += 20;
	}
	
	public void show(final int num){
//		num = 20;//编译不通过
		System.out.println(num);
	}
	
	
	public static void main(String[] args) {
		
		int num = 10;
		
		num = num + 5;
		
		FinalTest test = new FinalTest();
//		test.setDown(3);
		
		test.show(10);
	}
}


final class FinalA{
	
}

//class B extends FinalA{
//	
//}

//class C extends String{
//	
//}

class AA{
	public final void show(){
		
	}
}

class BB extends AA{
	
//	public void show(){
//		
//	}
}

 

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108990058