In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the final modifier in java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Definition of final modifier:
The final keyword can be used to modify classes, variables, and methods
When final modifies a variable, it means that once the initial value of the variable is obtained, it cannot be changed (strictly speaking: the variable modified by final cannot be changed, but once the initial value is obtained, the value of the final variable cannot be re-assigned)
Final can modify not only member variables (class variables and instance variables), but also local variables and formal parameters
Related Video Learning course: java online Learning
Second, the syntax of final member variables stipulates:
Final-modified member variables must be explicitly specified by the programmer, and final members are not implicitly initialized.
1. The initial values of class variables and instance variables modified by final are as follows:
Class variables: the initial value must be specified in the static initialization block or when the class variable is declared, and can only be specified in one of two places
Instance variable: the initial value must be specified in a non-static initialization block, declaration of the instance variable, or constructor, and can only be specified in one of three places
Note: if a normal initialization block has already specified an initial value for an instance variable, you can no longer set an initial value for that instance variable in the constructor
The following program demonstrates the effect of final decorating member variables:
Packagelextends
PublicclassFinalVariableTest {
/ / specify the default value when defining member variables, which is legal
Finalinta=6
/ / the following variables will assign initial values in the constructor or initialization block
FinalStringstr
Finalintc
Finalstaticdoubled
/ / neither the default value nor the initial value is specified in the initialization block or constructor
/ / the ch instance defined below is illegal
/ / finalcharch
/ / initialization block, which can specify an initial value for an instance variable that does not specify a default value
{
/ / specify the initial value for the instance variable in the initialization block.
Str= "Hello"
/ / the default value has been specified when defining the an instance variable
/ / A cannot be reassigned, so the following assignment statement is illegal
/ / axi9
}
/ / static initialization block, which can specify initial values for class variables that do not specify default values
Static {
/ / specify the initial value for the class variable in the static initialization block.
Dice 5.6
}
/ / constructor, you can specify an initial value for an instance variable that has neither a default value nor an initial value in the initialization block
PublicFinalVariableTest () {
/ / if an initial value has been specified for str in the initialization block
/ / then the final variable cannot be reassigned in the constructor, the following assignment statement is illegal
/ / str= "java"
Cedar 5
}
PublicvoidchangeFinal () {
/ / ordinary methods cannot assign values to member variables modified by final
/ / dong1.2
/ / you cannot specify initial values for final member variables in a normal method
/ / ch='a'
}
Publicstaticvoidmian (String [] args) {
FinalVariableTestft=newFinalVariableTest ()
System.out.println (ft.a)
System.out.println (ft.c)
System.out.println (ft.d);}
}
2. If you plan to initialize the final member variable in the constructor or initialization block, do not access the value of the member variable before initialization.
Packagelextends
PublicclassFinalErrorTest {
/ / define an instance variable modified by final
/ / final member variables will not be initialized by default
Finalintage
{
/ / age is not initialized, so the code here will cause an error because it attempts to access an uninitialized variable
/ / as long as the final modifier when defining age is removed, the program will be correct
System.out.println (age)
Age=6
System.out.println (age)
}
Publicstaticvoidmain (String [] args) {
NewFinalErrorTest ()
}
}
3. Final local variables
1, definition: the system will not initialize the local variable, the local variable must be initialized by the programmer. So when you use final to modify a local variable, you can either specify the default value at definition time or not.
The following program demonstrates that final modifies local variables and formal parameters:
Final modifies the case of formal parameters, because initialization is done by the system based on the parameters passed when the method is called, so the use of the final modifier cannot be assigned. )
Packagelextends
PublicclassFinalLocalVariable {
Publicvoidtest (finalinta) {
/ / the parameter modified by final cannot be assigned. The following statement is illegal
/ / axi5
}
Publicstaticvoidmian (String [] args) {
/ / if the default value is specified when defining a final local variable, the str variable cannot be reassigned
FinalStringstr= "hello"
/ / illegal assignment statement below
/ / str= "Java"
/ / if no default value is specified when defining a final local variable, the d variable can be assigned once
Finaldoubled
Dice 5.6
/ / A pair of final variables are reassigned. The following statement is illegal
/ / dong6.4
}
}
Fourth, final modifies the difference between basic type variables and reference type variables.
Reference type variables decorated with final cannot be reassigned, but you can change the contents of objects referenced by reference type variables
For example, for the array object referenced by the following iArr variable, the final modified iArr variable cannot be reassigned, but the array elements of the array referenced by iArr can be changed.
Eg.
/ / final modifies array elements. IArr is a reference variable.
Finalint [] iArr= {5,6,12,9}
System.out.println (Arrays.toString (iArr))
/ / sort a pair of array elements, legal
Arrays.sort (iArr)
System.out.println (Arrays.toString (iArr))
/ / assign elements to the array, legal
IArr [2] =-8
System.out.println (Arrays.toString (iArr))
/ / the following statement reassigns a value to iArr, which is illegal
/ / iArr=null
5. Final variables that can perform "macro substitution"
1. For a final variable, whether it is a class variable, an instance variable, or a local variable, as long as the variable satisfies three conditions, the final variable is no longer a variable, but a direct quantity.
(1) modify with final modifier
(2) the initial value is specified when defining the final variable.
(3) the initial value can be determined at compile time.
2. An important use of final modifiers is to define "macro variables". When the final variable is defined, the initial value is specified for the variable, and the variable can be determined at the time of the variable, then the final variable is essentially a "macro variable", and the compiler will directly replace all places in the program where the variable is used with the value of the variable.
3 、
Eg.
Strings1= "Crazy Java"
The string referenced by the / / S2 variable can be determined at compile time
/ / so S2 directly references the "crazy Java" string that already exists in the variable pool
Strings2= "Crazy" + "Java"
System.out.println (s1==s2); / / true
/ / define two string direct quantities
Stringstr1= "crazy"
Stringstr2= "Java"
Strings3=str1+str2
System.out.println (s1==s3); / / false
For S3, its value is obtained by connecting str1 and str2. Because str1 and str2 are just two ordinary variables, the compiler will not perform "macro replacement", so the compiler cannot determine the value of S3 and cannot make S3 point to the "crazy Java" cached in the string pool.
The above is all the content of the article "how to use final modifiers in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.