Reputation: 2268
what is the proper way of declaring constants array in a class? I have read post where ppl make a case for
public static final Strings[] words;
like here
however, I find that declaring your variables public, it means that the user(tester) can see the variables; even though, he can't change them because they are final... anyone could explain why should I declare the above constant instead of
private static final Strings[] words;
or just
private final Strings[] words;
thanks
Upvotes: 0
Views: 243
Reputation: 836
Since String [] words is final you can initialize it once I.e if you say string [] words=new string[5]
Or you initialized it any other way but once its initialized you can't make it to refer some other instance of string array.
but its element can be changed.
example words[0]="string1" can be changed to words[0]="string2"
If you say
private static final string[] words then words will become class variable and can be access within class.
if you say
private final string [] words then words will become instance variable and can be accessed within class
if you say variable as public then it can be accessed out side class and its element can be modified by any one.
Recommendation
1.Use enum
2. Use list and make it unmodifiable using Collections.unmodifiableList()
Upvotes: 0
Reputation: 932
Do you know what the static or final keyword does, as it does not show in your question!
Read about static & final first!
It should be static final as then only it'll actually hold some meaning for being a constant!
Btw, "Strings"??
Upvotes: 0
Reputation: 32407
Usually you shouldn't use arrays in Java, unless you really need the performance.
You should use a Collection
(or a List
if it's ordered) instead.
For a public constant,
public static final Collection<String> FIXED_STRINGS = Collections.unmodifiableCollection(Arrays.asList("string1","string2"));
Or private
, if it's not for use outside the class.
Upvotes: 4
Reputation: 13946
The example you point to talks about public String
constants, not public String[]
.
You can't make String[]
unchangeable. final
only means a user can't make the reference point to a different object. It does not prevent the user from changing the contents of the array. (final
in Java is very different (and less powerful) than const
in C++).
As for whether or not static
should be there, remember what the difference is. With the static
, the member will be shared by all instances of the class. Without the static
there will be a separate copy of the member in every instance of the class.
Generally when something is a true constant that is wasteful and so in such situations static
is generally used.
Upvotes: 1
Reputation: 2349
declaring a class variable public is discouraged because it does not make it clear that they are class variables.
it also opens your class to other people writing code that will be dependent on that specific class variable.
Upvotes: 0
Reputation: 11113
I prefer using enums to declare constants:
public enum Words {
ONE("one"),
TWO("two");
private String title;
private Words(String title){
this.title = title;
}
public String getTitle() {
return title;
}
}
Which is very convenient to use (Words.ONE) and you can associate other attributes with each property (title, etc).
You can also get the array of constants by calling Words.values();
Upvotes: 0
Reputation: 544
it's just depends what you want to do, like any object oriented concept.
public/private/... is the access you want to declare to your members/method.
BTW final indicate that you cannot change the variable once initialized. static means that all the instance of the class will have the same variable.
In short all is possible.
Upvotes: 0