K
Karsten Wutzke
Hi all!
I know this has been discussed, but I couldn't find the answers I was
looking for.
Final constants are supposed to be all UPPER_CASE using underscores.
For int's this is all ok. However, should you also name String's,
File's, Properties', JTextField's etc. in the same way?
Examples:
private static final String STR_APP_VERSION = "0.1a";
private static final String STR_VERSION =
System.getProperty("java.version");
private static final File FL_ROOT_DIR = new
File(System.getProperty("user.dir")).getAbsolutePath();
private static final String STR_IMAGES_DIR_NAME = "images";
private static final File FL_IMAGES_DIR = new File(FL_ROOT_DIR,
STR_IMAGES_DIR_NAME);
private static final Properties PRP = new Properties();
public static save(final UserData UD)
{
File flUserData = UD.getFile();
... //some anonymous inner class defined accessing UD
}
Anybody reading this should note I use some prefixing naming
convention for all *non-final* variables (the main reason for this
"Hungarian notation" is not having to think about new identifiers when
creating associated objects, main use is for all those JLabels,
JTextFields, JPanels, JButtons etc. in a GUI see last example):
String strFirstName = "Dale"; // ;-)
private File flSearchDir = new File(FL_ROOT_DIR, strDirName); //
strDirName might be a local var
Properties prpTempUserData = new Properties();
UserData udOld = UserData.getInstance();
JLabel lbAge = new JLabel("Age:");
SpinnerNumberModel snmAge = new SpinnerNumberModel(userAge, 16, 70,
1); //userAge is some int
spnAge = new JSpinner(snmAge);
JPanel pnAge = GuiFactory.createFlowPanel(lbAge, spnAge);
pn.add(pnAge);
As seen above, for all *final* class instance variables I try to keep
the equivalent prefix notation: STR_FIRST_NAME, FL_SEARCH_DIR,
PRP_TEMP_USER_DATA, TF_FIRST_NAME, ...
But the question is: Is it necessary? Is it recommended? All
discussions I read so far, including Sun's naming conventions propose
UPPER_CASE for all 'constants'. The examples I saw only include
*int*'s though.
How is a 'constant' defined anyway? Is it equivalent to 'final'? Why
should I name only int's uppercase and name all final *class
instances* in mixed case? To stay consistent I started naming all
final variables in UPPER_CASE, but anytime I change the final
attribute I also have to rename the variable, which only just wastes
my time...
Furthermore, naming all final variables of all class instances
UPPER_CASE somehow looks strange in my editor , especially when syntax
highlighting for final variables is on... I could change that anytime
of course, but the main question is, should I name all final variables
UPPER_CASE or just int's (and/or simple types)?
As an exception to the rule, I might consider not just naming final
int's UPPER_CASE etc. but also String's used *only* as keys for
identifying objects in HashMap's, the Preferences API etc.
I find that hard to decide...
Opinions?
TIA
Karsten
I know this has been discussed, but I couldn't find the answers I was
looking for.
Final constants are supposed to be all UPPER_CASE using underscores.
For int's this is all ok. However, should you also name String's,
File's, Properties', JTextField's etc. in the same way?
Examples:
private static final String STR_APP_VERSION = "0.1a";
private static final String STR_VERSION =
System.getProperty("java.version");
private static final File FL_ROOT_DIR = new
File(System.getProperty("user.dir")).getAbsolutePath();
private static final String STR_IMAGES_DIR_NAME = "images";
private static final File FL_IMAGES_DIR = new File(FL_ROOT_DIR,
STR_IMAGES_DIR_NAME);
private static final Properties PRP = new Properties();
public static save(final UserData UD)
{
File flUserData = UD.getFile();
... //some anonymous inner class defined accessing UD
}
Anybody reading this should note I use some prefixing naming
convention for all *non-final* variables (the main reason for this
"Hungarian notation" is not having to think about new identifiers when
creating associated objects, main use is for all those JLabels,
JTextFields, JPanels, JButtons etc. in a GUI see last example):
String strFirstName = "Dale"; // ;-)
private File flSearchDir = new File(FL_ROOT_DIR, strDirName); //
strDirName might be a local var
Properties prpTempUserData = new Properties();
UserData udOld = UserData.getInstance();
JLabel lbAge = new JLabel("Age:");
SpinnerNumberModel snmAge = new SpinnerNumberModel(userAge, 16, 70,
1); //userAge is some int
spnAge = new JSpinner(snmAge);
JPanel pnAge = GuiFactory.createFlowPanel(lbAge, spnAge);
pn.add(pnAge);
As seen above, for all *final* class instance variables I try to keep
the equivalent prefix notation: STR_FIRST_NAME, FL_SEARCH_DIR,
PRP_TEMP_USER_DATA, TF_FIRST_NAME, ...
But the question is: Is it necessary? Is it recommended? All
discussions I read so far, including Sun's naming conventions propose
UPPER_CASE for all 'constants'. The examples I saw only include
*int*'s though.
How is a 'constant' defined anyway? Is it equivalent to 'final'? Why
should I name only int's uppercase and name all final *class
instances* in mixed case? To stay consistent I started naming all
final variables in UPPER_CASE, but anytime I change the final
attribute I also have to rename the variable, which only just wastes
my time...
Furthermore, naming all final variables of all class instances
UPPER_CASE somehow looks strange in my editor , especially when syntax
highlighting for final variables is on... I could change that anytime
of course, but the main question is, should I name all final variables
UPPER_CASE or just int's (and/or simple types)?
As an exception to the rule, I might consider not just naming final
int's UPPER_CASE etc. but also String's used *only* as keys for
identifying objects in HashMap's, the Preferences API etc.
I find that hard to decide...
Opinions?
TIA
Karsten