1. Basic Considerations for constant definition.
In Java, the final keyword (flexible use of static keywords in Java classes) is used to define constants. When a constant is set, it is generally not allowed to be changed. You can use the following form to define a constant: Final double Pi = 3.1315. Note the following when defining this constant:
First, constants must be initialized when defined. That is, it must be initialized during constant declaration. This is different from local variables or member variables. After the constant is initialized during definition, the constant cannot be assigned again in the application. If a value is forcibly assigned, the database jumps out of the error message and rejects the new value. (The access method of constants defined in the interface)
Second, the scope of use of final keywords. This final keyword can be used not only to modify constants of the basic data type, but also to modify object references or methods. For example, an array is an object reference. Therefore, you can use the final keyword to define an array of constants. This is a major feature of Java. Once an array object is set as a constant array by the final keyword, it can only point to one array object at a time and cannot point it to another object, you cannot change the value of an array (the binary search algorithm that can be used to insert an ordered array.
Third, pay attention to the naming rules of constants. Different Languages have their own encoding rules when defining variables or constants. This is mainly to improve code sharing and ease of coding. In Java, when defining constants, you also have a set of rules. For example, constants are generally named in uppercase. In Java, uppercase and lowercase characters are sensitive. Uppercase characters are used to distinguish them from variables. Although the name of a constant is given in lower case, there will be no syntax errors. However, to clearly identify variables and constants when writing code, it is best to set constants to uppercase characters. In addition, constants often use underscores to separate different characters. Instead of separating object names or class names in uppercase. Although these rules are not mandatory, they must be followed to improve code friendliness and facilitate reading by other members of the development team. No rules.
In short, Java developers should note that constants defined as final must be named in uppercase letters, and it is best to use underscores as separators to connect multiple words. Data defining final, whether constant, object reference, or array, cannot be changed in the main function. Otherwise, the editor rejects the request and prompts an error message.Ii. FinalKeyword and staticKeyword.
Java is an object-oriented language, so it is different from other programming languages when defining constants. For example, if the code of a program is edited to the final execution, the code is loaded and the object is created in two processes. Different processes have different influences on constants. Assume that the following code is available:
Private Static random rd1 = new random (); // instantiate a random number to generate an object.
Private Final int int1 = rd1.nestint (10); // generate a random number and assign it to the constant int1
Private Static final int int2 = rd1.nestint (10); // generate a random number and assign it to the constant int2
The preceding statement roughly indicates that a random number is generated through a random number Class Object provided by Java. Assign the generated random number to the constants int1 and int2. Careful readers will find that, although it is also a value assignment statement, there is a small difference between the two statements, that is, there is a keyword static in the second statement. I have mentioned the use of keywords in my previous articles. This is a static concept. That is, when you use this keyword to modify a variable, a bucket is created for the variable before the object is created. If this static variable is required for the object to be created later, the storage space of this variable will be shared. That is to say, if this variable is used during object creation, the system will not assign a bucket to it, but assign the address of the bucket to it. The advantage of doing so is that multiple objects can adopt the same initial variable. You only need to change the variable value of multiple objects once. In terms of this feature, it is similar to a constant. However, it cannot replace constants.
So what are the differences between the above two statements? First, let's look at the statement private final int int1 = rd1.nestint (10. Although int1 is also a constant, it is initialized when the object is created. To create two objects, initialize the variable twice. During the two object initialization processes, the values of constant initialization are also different because the random numbers generated are different. The final result is that, although int1 is a constant, its values may be different in different objects. It can be seen that the constant defined as final is not constant. By default, the defined constant is initialized when the object is created. If you assign a fixed value directly when creating a constant instead of assigning values through other objects or functions, the value of this constant is constant, that is, make the same value in multiple objects. However, when assigning values to constants, some functions or objects are used (such as random number random objects ), the initialization value for the constant may be different each time an object is created. This is often not desired by developers. Sometimes a program developer wants to create more objects. The reference constant values of multiple objects are the same.
There are two ways to achieve this. First, assign a fixed value, such as ABCD, to a constant. Instead of a function or object that will change according to the environment. For an object that generates a random number, the results of each running operation may be unavailable. When this object is used to initialize constants, The results may be different each time an object is created. In the end, this constant can only be constant within an object, but not in an application. Another method is to use both the static keyword and the final keyword. An object reference or constant defined as final can only point to a unique object. It cannot point to other objects. However, as in the preceding example of a random number, the value of the object content can be changed. To ensure that a constant is not changed in an application, you need to declare the constant as a constant of staitc final. What does this mean? As I mentioned above, when executing an application, it can be divided into two steps: Code loading and object creation. To ensure that the application can still get a constant with the same value in all circumstances (that is, when multiple objects are created), it is best to tell the compiler, initialize the constant value during code loading. Then, when creating an object in the future, only the address of this constant object will be referenced, instead of re-initializing it. Define constants in the form of Private Static final int int2 = rd1.nestint (10. In this way, after the object is created multiple times, the int2 value of this constant is the same. This is because the constant is referenced only when an object is created, and the constant is not initialized again.
Because the static keyword is added, it is equivalent to changing the scope of the constant. For this reason, developers need to understand their own needs and then choose whether to use this keyword. When initializing a constant, if you use a function (such as the current time of the system) or an object (such as an object that generates a random number) to initialize the constant, it is foreseeable that different values may be obtained each time this constant is initialized, so you need to consider whether to use this static keyword. In general, if you only need to ensure that this constant is used inside the object, this keyword is dispensable. However, if you need to reference this constant in multiple objects and have the same value, you must use the static keyword. To ensure that each object has only one constant value. In other words, constants referenced in different objects actually point to the same region in the memory.