Static final variable java example:

  • If we declare any variable as static means it is class level variable.
  • If we declare any variable as static final its class level variable and its value can not be changed 
  • final static variables are constants in java.
  • We can access these final static variables directly without using object or class name.
  • We can not change the value of final static variables in any object.

java example program on final static variable 

  1. Class A{
  2.  
  3. public static final String str="Learn Java Online";
  4.  
  5.  public static void main(String[]  args){ 

  6. System.out.println(str); 
  7.  //str="j2ee Interview Questions"; gives error

  8. }
  9. }

Output:
  1. Learn Java Online

Static String:

  • static Strings are class level strings.
  • we can access these static Strings using class name.


  1. Class A{
  2.  
  3. public static String str="Learn Java Online";
  4.  
  5.  public static void main(String[]  args){ 

  6. System.out.println(A.str); 
  7. System.out.println(str);
  8.  

  9. }
  10. }



Output:
  1. Learn Java Online
  2. Learn Java Online

Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu