Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

final

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?

like image 672
storm_buster Avatar asked Jul 26 '12 20:07

storm_buster


People also ask

When would you use a static string?

Use public final static String when you want to create a String that: belongs to the class ( static : no instance necessary to use it), that. won't change ( final ), for instance when you want to define a String constant that will be available to all instances of the class, and to other objects using the class, and ...

Should static final variables be public?

Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables.

What is the use of public static final in Java?

The static keyword means the value is the same for every instance of the class. The final keyword means once the variable is assigned a value it can never be changed. The combination of static final in Java is how to create a constant value.

Why should a variable be declared as static and final?

Declaring variables only as static can lead to change in their values by one or more instances of a class in which it is declared. Declaring them as static final will help you to create a CONSTANT. Only one copy of variable exists which can't be reinitialize.


1 Answers

final indicates that the value of the variable won't change - in other words, a constant whose value can't be modified after it is declared.

Use public final static String when you want to create a String that:

  1. belongs to the class (static: no instance necessary to use it), that
  2. won't change (final), for instance when you want to define a String constant that will be available to all instances of the class, and to other objects using the class, and that
  3. will be a publicly accessible part of the interface that the class shows the world.

Example:

public final static String MY_CONSTANT = "SomeValue";  // ... in some other code, possibly in another object, use the constant: if (input.equals(MyClass.MY_CONSTANT) 

Similarly:

public static final int ERROR_CODE = 127; 

It isn't required to use final, but it keeps a constant from being changed inadvertently during program execution, and serves as an indicator that the variable is a constant.

Even if the constant will only be used - read - in the current class and/or in only one place, it's good practice to declare all constants as final: it's clearer, and during the lifetime of the code the constant may end up being used in more than one place.

Furthermore using final may allow the implementation to perform some optimization, e.g. by inlining an actual value where the constant is used.

Finally note that final will only make truly constant values out of primitive types and String which is immutable. Applying final to an object (for instance a HashMap) will make the reference immutable, but not the state of the object: for instance data members of the object can be changed, array elements can be changed, and collections can be manipulated and changed.

like image 73
pb2q Avatar answered Oct 20 '22 10:10

pb2q