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

前端 未结 11 1182
你的背包
你的背包 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 12:42

    You do not have to use final, but the final is making clear to everyone else - including the compiler - that this is a constant, and that's the good practice in it.

    Why people doe that even if the constant will be used only in one place and only in the same class: Because in many cases it still makes sense. If you for example know it will be final during program run, but you intend to change the value later and recompile (easier to find), and also might use it more often later-on. It is also informing other programmers about the core values in the program flow at a prominent and combined place.

    An aspect the other answers are missing out unfortunately, is that using the combination of public final needs to be done very carefully, especially if other classes or packages will use your class (which can be assumed because it is public).

    Here's why:

    1. Because it is declared as final, the compiler will inline this field during compile time into any compilation unit reading this field. So far, so good.
    2. What people tend to forget is, because the field is also declared public, the compiler will also inline this value into any other compile unit. That means other classes using this field.

    What are the consequences?

    Imagine you have this:

    class Foo {
      public static final String VERSION = "1.0";
    }
    
    class Bar {
      public static void main(String[] args) {
        System.out.println("I am using version " + Foo.VERSION);
      }
    }
    

    After compiling and running Bar, you'll get:

    I am using version 1.0
    

    Now, you improve Foo and change the version to "1.1". After recompiling Foo, you run Bar and get this wrong output:

    I am using version 1.0
    

    This happens, because VERSION is declared final, so the actual value of it was already in-lined in Bar during the first compile run. As a consequence, to let the example of a public static final ... field propagate properly after actually changing what was declared final (you lied!;), you'd need to recompile every class using it.

    I've seen this a couple of times and it is really hard to debug.

    If by final you mean a constant that might change in later versions of your program, a better solution would be this:

    class Foo {
      private static String version = "1.0";
      public static final String getVersion() {
        return version;
      }
    }
    

    The performance penalty of this is negligible, since JIT code generator will inline it at run-time.

    0 讨论(0)
  • 2020-12-07 12:45

    The keyword final means that the value is constant(it cannot be changed). It is analogous to const in C.

    And you can treat static as a global variable which has scope. It basically means if you change it for one object it will be changed for all just like a global variable(limited by scope).

    Hope it helps.

    0 讨论(0)
  • 2020-12-07 12:45

    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.

    0 讨论(0)
  • 2020-12-07 12:47

    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.

    0 讨论(0)
  • 2020-12-07 12:49

    static means that the object will only be created once, and does not have an instance object containing it. The way you have written is best used when you have something that is common for all objects of the class and will never change. It even could be used without creating an object at all.

    Usually it's best to use final when you expect it to be final so that the compiler will enforce that rule and you know for sure. static ensures that you don't waste memory creating many of the same thing if it will be the same value for all objects.

    0 讨论(0)
  • 2020-12-07 12:53

    It is kind of standard/best practice. There are already answers listing scenarios, but for your second question:

    Why do they have to do that? Why do they have to initialize the value as final prior to using it?

    Public constants and fields initialized at declaration should be "static final" rather than merely "final"

    These are some of the reasons why it should be like this:

    1. Making a public constant just final as opposed to static final leads to duplicating its value for every instance of the class, uselessly increasing the amount of memory required to execute the application.

    2. Further, when a non-public, final field isn't also static, it implies that different instances can have different values. However, initializing a non-static final field in its declaration forces every instance to have the same value owing to the behavior of the final field.

    0 讨论(0)
提交回复
热议问题