Java Final and final static Constants

This Java example page uses the final keyword to indicate constant fields and local variables. A field can be final static.
Final. A value cannot be changed. For example 10 is 10. We cannot change 10 to 20. With a variable, we can assign new values—but a value is final.
With the final keyword, we indicate a field or local is constant (a value). A final thing is in its terminal state. It cannot ever become something new.
First example. Here we introduce a class with 2 final ints on it. The Box class has width and height final values on it. These can be accessed like fields.

Tip: We can use one constant to specify another constant. Here the "height" constant is based on the final width constant.

Java program that uses final on class class Box { // This class has final ints on it. final int width = 10; final int height = width * 2; public int area() { return width * height; } } public class Program { public static void main(String[] args) { // Create and use class with final ints. Box box = new Box(); System.out.println(box.area()); } } Output 200
Final static. In Java the static keyword means a field that is based on the type, not the instance of the type. It only exists once at a time in memory.

Here: We use a final static string. This is a constant that can be accessed on the type, not an instance of the type. It cannot be changed.

Java program that uses final static String public class Program { // This String is both final and static. final static String description = "Hello world program"; public static void main(String[] args) { // Print string. System.out.println(description); } } Output Hello world program
Final String. Here we use a final String. We then create a string with the same value (Hyperion) and compare the two. We must use equals() to compare two strings based on data.EqualsStrings
Java program that uses final String public class Program { public static void main(String[] args) { // This is a final String. final String name = "Hyperion"; // Construct another String. String test = "Hyper"; String test2 = test + "ion"; System.out.println(name); // Equals test the two strings. System.out.println(name.equals(test2)); // The string references are different. System.out.println(name == test2); } } Output Hyperion true false
Final error. We cannot assign to a final value. We receive a "cannot be assigned" error in this case. The program will not compile.
Java program that uses final int public class Program { public static void main(String[] args) { final int size = 10; // This fails with a compilation error. size = 200; } } Output The final local variable size cannot be assigned. It must be blank and not using a compound assignment
A terminal review. The final keyword is not related to the finally keyword. Final indicates constancy. Meanwhile finally is part of an exception handling construct.Exceptions
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls