Home  >  Article  >  Java  >  How to use constants in Java

How to use constants in Java

藏色散人
藏色散人Original
2019-01-19 10:13:127562browse

A constant is a variable whose value cannot be changed once assigned. Java has no built-in support for constants, but the variable modifiers static and final can be used to create constants efficiently.

Constants can make your program easier to read and understand by others. Additionally, both the JVM and the application cache a constant, so using constants can improve performance.

How to use constants in Java

static modifier

This allows variables to be used without creating an instance of the class; static class members are associated with the class itself association, rather than being associated with the object. All class instances share the same copy of the variable.

This means that another application or main() can easily use it.

For example, class myClass contains a static variable days_in_week:

public class myClass {
   static int days_in_week = 7;
}

Because this variable is static, it can be used elsewhere without explicitly creating a myClass object:

public class myOtherClass {  
   static void main(String[] args) {
       System.out.println(myClass.days_in_week);
   }
 }

final modifier

The final modifier means that the value of the variable cannot be changed. Once a value is assigned, it cannot be reassigned.

You can use the final modifier to make primitive data types (i.e. int, short, long, byte, char, float, double, boolean) immutable/unchangeable.

These modifiers together create a constant variable.

static final int DAYS_IN_WEEK = 7;

Note that after adding the final modifier, we declare DAYS_IN_WEEK in all caps. It is a long-standing practice among Java programmers to define constant variables with capital letters and to separate words with underscores.

This format is not required for Java, but it makes it easier for anyone reading the code to immediately identify the constants.

Potential Problems with Constant Variables

In Java, the way the final keyword works is that the pointer to the value of the variable cannot be changed. Let us repeat: it is a pointer that cannot change the location it points to.

There is no guarantee that the referenced object will remain unchanged, only that the variable will always hold a reference to the same object. If the referenced object is mutable (i.e. has fields that can be changed), then a constant variable may contain a different value than the one originally assigned.

The above is the detailed content of How to use constants in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What is Java overloadingNext article:What is Java overloading