Home > Java > javaTutorial > body text

final variable in java

WBOY
Release: 2023-09-24 20:49:02
forward
1164 people have browsed it

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
   }
}
Copy after login

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

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template