When I define constant values in my Java code, I generally declare them 'private static final', but recently I've been maintaining code where the constants are defined 'private final'.
I'm optimizing at the moment and was wondering whether to 'static'ize these.
For example
public class X {
private final String SOME_CONST = "Whatever";
}
Is the above code equivalent (at run-time) to the following, so only 1 copy of 'SOME_CONST' is held?
public class X {
private static final String SOME_CONST = "Whatever";
}
I would have thought this was fairly basic, but I can't find the answer anywhere.
[Edit] Some people have answered on the String instance being interned. Sorry, I should have picked a better example, in the case I'm looking at, it's not just Strings, but a lot of different types (some standard, some user defined).
I'm more interested in the effects of the 'private final' versus a 'private static final' declaration.