Variables that final can modify in Java
•
Java
Here we only discuss the variables that final can modify, not methods and classes.
1. Final can modify variables: class variables, member variables, and local variables.
2. It should be noted that:
(1) Differences: modify class variables and member variables: initial values must be assigned when declaring variables;
Modified local variable: it does not need to be assigned an initial value. Because you must assign a value to a local variable before using it, it is illegal to directly call an unassigned local variable, so the local variable modified by final can be declared without assignment.
(2) Same point: variables modified by final can only be assigned once.
3. Example code:
错误提示:变量a1可能没有被初始化 //final static int a2;//错误提示:变量a2可能没有被初始化 final int a3=1; final static int a4=2; private void testFinal(){ final int a5; a5=1; //a5=2;//错误提示:变量a5可能已经被赋值. } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码