TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Final and final static Constants

Use 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
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf