final keyword can be modified to different content, which includes only one variable, a method, or a class, the following are final modified the content of the role:
final variable: indicates that the variable is a constant that can only be initiated once a final method: Indicates that the the method can not be overridden
final class: this class can not be inherited represents the
final variable
when a variable is modified final keyword means that its value can not be changed, that is to say, this variable represents a constant. This also means that the final variable must be initialized. Is a reference to an object if a final variable represents, then that variable will not be able to re-express reference to another object, but the final internal state variable points to the object can be changed, for example, we can array to a final or final collection of add or delete elements. In addition, the use of capital letters and underlined a final variable is a good habit.
Examples
// A variable Final
Final the THRESHOLD = int. 5;
// A blank variable Final
Final int the THRESHOLD;
// A static variable Final the PI
static Double Final the PI = 3.141592653589793; // A static variable blank Final
static Double Final the PI;
initialize a final variables
final variable - Meng Pui tree without Kengpian, we must initialize a final variable, or the compiler will throw an error. A final variable can be initialized only once, it can be initialized by initializing the device or its initialization statement. There are three ways to initialize a final variable:
to align initialized when they are declared final variable, which is the most common way to initialize. When a final variable is not initialized when declared, this variable is to become blank final variable, then the following two methods is to initialize a blank final variable blank finalfinal variable - Moe no Kengpian Bay Tree, variables can code consists instance-initializer block or constructor initializes. If you have multiple constructors, then the blank final variable must be initialized in every constructor, otherwise a compile-time error.
A blank static final variables can be initialized Examples static code block inside
// Initialize method final variable
class the Test {
// Initialization directly
final int = the THRESHOLD. 5;
// blank final variable
final int in CAPACITY;
final int maxmum;
// Blanck static final variable
static final int MINIMUM;
// use initializer
{
CAPATICY = 25;
}
// use the static block
static {
= 125 maxmum;
}
// constructor using
public the Test () {
MINIMUM = -1;
}
}
final variable - Moe no Kengpian Bay Tree
Guess you like
Origin blog.51cto.com/14539425/2440381
Recommended
Ranking