when exactly are we supposed to use “public static final String”?

前端 未结 11 1193
你的背包
你的背包 2020-12-07 12:08

I have seen much code where people write public static final String mystring = ... and then just use a value.

Why do they have to do that? Why do they h

11条回答
  •  一向
    一向 (楼主)
    2020-12-07 13:01

    1. Static means..You can use it without instantiate of the class or using any object.
    2. final..It is a keyword which is used for make the string constant. You can not change the value of that string. Look at the example below:

        public class StringTest { 
                 static final String str = "Hello"; 
        public static void main(String args[]) { 
                 // str = "world"; // gives error 
                 System.out.println(str); // called without the help of an object                       
                 System.out.println(StringTest.str);// called with class name  
                   } 
               } 
      

    Thanks

提交回复
热议问题