when exactly are we supposed to use “public static

2019-01-16 06:10发布

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 have to initialize the value as final prior to using it?

UPDATE

Ok, thanks all for all your answers, I understand the meaning of those key (public static final). What I dont understand is why people use that even if the constant will be used only in one place and only in the same class. why declaring it? why dont we just use the variable?

标签: java final
9条回答
男人必须洒脱
2楼-- · 2019-01-16 06:29

public makes it accessible across the other classes. You can use it without instantiate of the class or using any object.

static makes it uniform value across all the class instances. It ensures that you don't waste memory creating many of the same thing if it will be the same value for all the objects.

final makes it non-modifiable value. It's a "constant" value which is same across all the class instances and cannot be modified.

查看更多
SAY GOODBYE
3楼-- · 2019-01-16 06:37
  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

查看更多
我命由我不由天
4楼-- · 2019-01-16 06:41

Usually for defining constants, that you reuse at many places making it single point for change, used within single class or shared across packages. Making a variable final avoid accidental changes.

查看更多
叛逆
5楼-- · 2019-01-16 06:41

Why do people use constants in classes instead of a variable?

readability and maintainability,

having some number like 40.023 in your code doesn't say much about what the number represents, so we replace it by a word in capitals like "USER_AGE_YEARS". Later when we look at the code its clear what that number represents.

Why do we not just use a variable? Well we would if we knew the number would change, but if its some number that wont change, like 3.14159.. we make it final.

But what if its not a number like a String? In that case its mostly for maintainability, if you are using a String multiple times in your code, (and it wont be changing at runtime) it is convenient to have it as a final string at the top of the class. That way when you want to change it, there is only one place to change it rather than many.

For example if you have an error message that get printed many times in your code, having final String ERROR_MESSAGE = "Something went bad." is easier to maintain, if you want to change it from "Something went bad." to "It's too late jim he's already dead", you would only need to change that one line, rather than all the places you would use that comment.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-16 06:44

final indicates that the value cannot be changed once set. static allows you to set the value, and that value will be the same for ALL instances of the class which utilize it. Also, you may access the value of a public static string w/o having an instance of a class.

查看更多
ら.Afraid
7楼-- · 2019-01-16 06:47

public makes it accessible across other classes. static makes it uniform value across all the class instances. final makes it non-modifiable value. So basically it's a "constant" value which is same across all the class instances and which cannot be modified. With respect to your concern "What I don't understand is why people use that even if the constant will be used only in one place and only in the same class. Why declaring it? Why don't we just use the variable?" I would say since it is a public field the constant value can also be used elsewhere in some other class using ClassName.value. eg: a class named Math may have PI as final static long value which can be accessed as Math.PI.

查看更多
登录 后发表回答