From the course: Java 8+ Essential Training: Objects and APIs

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use static fields as constants

Use static fields as constants - Java Tutorial

From the course: Java 8+ Essential Training: Objects and APIs

Start my 1-month free trial

Use static fields as constants

- [Instructor] In general programming terminology, a constant is a value that gets set once and then never changes. In Java, there isn't a constant keyword. Instead, you mark constant values with the combination of the keywords "static" and "final." "Static" means what it always does, that a variable belongs to the class definition and not to an instance of the class, and "final" means that the value can only be set once. IntelliJ IDEA makes it easy to declare simple constants. I'm going to place the cursor in my MathHelper class, which I've simplified a bit so it only has one constructor, and then, at the top of the class, before the instance variable declarations, I'll type "psfs." That means "public static final string." I'll press Tab to complete that string, and then I'll create a new constant. By convention, names of constants in Java are always all uppercase. The value of the constant doesn't matter. What's important is that it's used consistently throughout the application…

Contents