tampurus

37 Use of final keyword

May 2nd, 2022 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.49 KB | None | 0 0
  1. /*
  2. the final variable cannot be reinitialized with another value
  3. the final method cannot be overridden
  4. the final class cannot be extended
  5. */
  6. // reinitiating the value of final variable
  7. class Main {
  8.   public static void main(String[] args) {
  9.  
  10.     // create a final variable
  11.     final int AGE = 32;
  12.  
  13.     // try to change the final variable
  14.     AGE = 45;
  15.     System.out.println("Age: " + AGE);
  16.   }
  17. }
  18. /*
  19. output
  20. cannot assign a value to final variable AGE
  21.     AGE = 45;
  22.     ^
  23. 1 error
  24. */
Add Comment
Please, Sign In to add comment