Puneet Chaurasia Puneet Chaurasia - 11 months ago 93
Java Question

private static final String or private final String

For a Singleton class, is it a good idea to keep String constants (used internally in the member functions) as

private static final
private final

I want to know more in terms of difference in class's memory footprint for a long running application and otherwise.

Note : I know how
affect a variable. My question is specific to Constants that are used as
to the class. More specifically - about there lifetime. Even when there is no object of that class lying in memory.

Answer Source

There is not much difference between those, only that the private static final String gets initialized when the program gets initialized, the private final String gets initialized when the Singletons instance is being created.

The only advantage of the static String is, is that you can use it in static methods too (if you need them)

Also there are no performance issues with both of them, as both variables won't get garbage collected as long as the program is running.