Saturday 24 June 2017

what is the difference between public static final and public final static

what is the difference between public static final and public final static

No difference at all

They are the same. The order of modifiers is not significant. And note that the same rule applies in all contexts where modifiers are used in Java.

However, most Java style guides recommend/mandate the same specific order for the modifiers.
In this case, it is public static final.


private static final String API_RTN_ERROR= "1";
private final static String API_RTN_ERROR= "1";
static private final String API_RTN_ERROR= "1";
static final private String API_RTN_ERROR= "1";
final static private String API_RTN_ERROR= "1";
final private static String API_RTN_ERROR= "1";

even all above are same the position of first three is intercangeable.

Does it differ for private or public?
No, you can use any order in private and public. Just difference is private variables will not be accessible outside of class directly.


https://stackoverflow.com/questions/11219556/difference-between-final-static-and-static-final

what is the meaning of public static final string SOME_VARIABLE = "variable text"

what is the meaning of public static final string SOME_VARIABLE = "variable text"

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

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

belongs to the class (static: no instance necessary to use it), and 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.


Similarly:

public static final int ERROR_CODE = 54654;
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.

Also, you may access the value of a public static string w/o having an instance of a class.

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.

public makes it accessible across other classes.


what-is-difference-between-public static final and public final static