Home  >  Article  >  Java  >  final variable in java

final variable in java

WBOY
WBOYforward
2023-09-24 20:49:021113browse

final variable in java

#Final variables can only be explicitly initialized once. A reference variable declared as Final can never be reassigned to reference a different object.

However, the data within the object can be changed. Therefore, the state of the object can change, but the reference cannot.

For variables, the final modifier is usually used with static to make the constant a class variable.

Example

public class Test {
   final int value = 10;
   // The following are examples of declaring constants:
   public static final int BOXWIDTH = 6;
   static final String TITLE = "Manager";
   public void changeValue() {
      value = 12; // will give an error
   }
}

The above is the detailed content of final variable in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete