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 - Java Tutorial
From the course: Java 8+ Essential Training: Objects and APIs
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
About encapsulation3m 46s
-
Use the Java runtime classes4m 45s
-
(Locked)
Wrap code in static methods5m 56s
-
(Locked)
Declare and use custom classes4m 9s
-
(Locked)
Organize code with packages3m 46s
-
(Locked)
Create and use instance methods2m 27s
-
(Locked)
Manage state with instance variables7m 31s
-
(Locked)
Declare multiple constructor methods5m 2s
-
(Locked)
Use static fields as constants5m 51s
-
(Locked)
Declare and use enum types3m 44s
-
(Locked)
Organize code with nested types4m 15s
-
(Locked)
-
-
-
-
-