From the course: Java 11+ Essential Training

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 11+ Essential Training

Start my 1-month free trial

Use static fields as constants

- [Instructor] In programming terminology, a constant is a variable whose value can't be changed. In many programming languages, there's a special kind of variable called a constant. Some languages, for example, have a special keyword called C-O-N-S-T, const, that's used to declare a constant. Java doesn't have that, but you can create constant values in Java, using a combination of keywords. When you create a constant, typically you set it as static and final, meaning that it's a member of the class, not a class instance, and it's final, meaning it can't be changed. Let's take the example of the application I've been working on. I'm creating a new clothing item, and I'm using a string value of Shirt, because this is a literal string, it's easy to get wrong. I can mistype it, and the compiler won't have any way of checking to make sure that I typed it correctly. So, I want to setup some constants in my ClothingItem…

Contents