Skip to Content

Java Final variable example

  1. /*
  2.         Java Final variable example
  3.         This Java Example shows how to declare and use final variable in
  4.         a java class.
  5. */
  6.  
  7.  
  8. public class FinalVariableExample {
  9.  
  10.         public static void main(String[] args) {
  11.                
  12.                 /*
  13.                  * Final variables can be declared using final keyword.
  14.                  * Once created and initialized, its value can not be changed.
  15.                  */
  16.                 final int hoursInDay=24;
  17.                
  18.                 //This statement will not compile. Value can't be changed.
  19.                 //hoursInDay=12;
  20.                
  21.                 System.out.println("Hours in 5 days = " + hoursInDay * 5);
  22.                
  23.         }
  24. }
  25.  
  26. /*
  27. Output would be
  28. Hours in 5 days = 120
  29. */