Results 1 to 5 of 5
- 05-20-2010, 02:23 AM #1
need opinion on public static final etc ...
Everything works as intended. I am looking for conceptual advise. Am i doing it the right way from design stand point?
Suppose acontains the following declarations:Java Code:public class A
So why public, why final, why static?Java Code:// delimiters public final static char DELIMITER = '\u0002'; public final static char ERROR_SIGN = '\u0003';
Idea was that since A is the first class that encounters these variables, it is a right place to declare them at. It is public, because going in, i thought other classes will take advantage of this declaration. It is static because only a single instance of it exists in my universe and it is final, because it will never change
That being said, after declaration, public class A later continues to use them locally.
As mentioned before, this first time these variables are introduced ... Until set here at A, no other class needs to worry about them.
Moving on .. here comesSay it uses A.DELIMITER 20 times and A.ERROR_SIGN 10 timesJava Code:class B
Moving on .. here comesThis guy uses these delimiters to split a string 300,000,000 (not really, but plenty) times so it does thisJava Code:class C
After this class C continues to refer to local errorSign and delimiterJava Code:// do translation to String once instead of tons of times below private String errorSign = (Character.toString(A.ERROR_SIGN)); private String delimiter = (Character.toString(A.DELIMITER));
And so, i wonder ... is this a right approach? Should class A be the class where public static final vars are declared? Should i make a new separate class for this? Is approach i followed in class C appropriate?
Generally speaking, how do you deal with something like this?
Please advise and thanks for reading
- 05-20-2010, 02:47 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
the approach is good. Keeping all constants in one place is a good practice. I would move string constants over there as well:
and then everyone usesJava Code:public class MyConstants { private MyConstants() { // don't want anyone to make instances of this } public final static char DELIMITER = '\u0002'; public final static char ERROR_SIGN = '\u0003'; public final static char STRING_DELIMITER = Character.toString(DELIMITER); public final static char STRING_ERROR_SIGN = Character.toString(ERROR_SIGN); }
etc.Java Code:MyConstants.STRING_DELIMITER
One thing with this is that typing "MyConstants." everywhere is pretty tedious. Here is what you can do to avoid that:
Java Code:public [B]interface[/B] MyConstants { // yes, you can define constants in an interface! public final static char DELIMITER = '\u0002'; public final static char ERROR_SIGN = '\u0003'; public final static char STRING_DELIMITER = Character.toString(DELIMITER); public final static char STRING_ERROR_SIGN = Character.toString(ERROR_SIGN); } class A implements MyConstants { public void stuff() { if('!' == DELIMITER) { //... } } }
- 05-20-2010, 02:57 AM #3
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Or how about considering using an enum here?
- 05-20-2010, 03:15 AM #4
Nothing short of "awesome" Thanks Iljuxa. спасибо
- 05-20-2010, 09:54 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Public static void main (String args[])
By arefeh in forum New To JavaReplies: 12Last Post: 01-28-2010, 11:58 AM -
Accessing non-static public variables from another class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-22-2009, 05:45 PM -
Public static method error
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 11:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks