I have a class which has several public static final Strings that I use to store constants. When I am in another class and need to refer to these constants, what is the better way to do so? Service service = new Service(); // need this instance of service to do other things a) System.out.println(service.CONSTANT1); b) System.out.println(Service.CONSTANT1); I think I have read somewhere that b is the more appropriate means of referring to the constants, but I can't remember for sure. Your quick response is greatly appreciated. Thanks.
Joined: Feb 06, 2003
static variables are loaded when the class first loaded in the runtime environmet (i.e. no need to create an instance of the class) if you need only the constant value, I believe b is a better option.
Joined: Jan 23, 2003
verduka, There are no constants in Java which are analogous to C++ constants. So is the case with enumerations. As a best practice, it is preferrable to have constants in an interface like this -
Enumerations can also be done in the following manner -
Note : Above pieces of code are uncompiled and untested. And, these are subjective matter. I am sure there are other opinions out there on how these should be handled. It would be interesting to know them. -GB.
Joined: Dec 06, 2001
I prefer to use method (b) even if I already have an instance of the class. I think this is regarded as the better method, but that is subjective.
What I'm looking to implement is the "best practice". I understand that it is somewhat subjective, so I guess I'm trying to get a consensus of what most programmers would prefer. Personally, my instinct was exactly what Layne Lund said. Are there any performance impacts based on which implementation is used?
Joined: Jul 11, 2001
Originally posted by verduka fox: What I'm looking to implement is the "best practice". I understand that it is somewhat subjective, so I guess I'm trying to get a consensus of what most programmers would prefer. Personally, my instinct was exactly what Layne Lund said. Are there any performance impacts based on which implementation is used?
No, the two statements are fully equivalent semantically - the same byte code should get generated. IIRC, the constants even get inlined at compile time.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jan 07, 2003
Verduka, Its all a matter of personal preference, static variables are accesible both through object names and through the class name. However, it is much more useful to refer to instance variables by object names and static variables through class names, so that it is clear to the programmer right off what the variable is and why it behaves the way it does. for example static int i=20; int j=21; if you have say Obj a=new Obj(); Obj b=new Obj(); a.i=21; System.out.println(a.i); b.i=19; System.out.println(a.i); One might immediately guess that a.i is still 21 since only the value of i in oObject b has changed. But, of course since i is static, it may be confusing why a.i is now 19 and not 21. a.j=21; System.out.println(a.j); b.j=19; System.out.println(a.j); a.j is now 19 and not 21 because j is an instance variable. As to which method iis more efficient, both are equally efficient. Hope its clear.
May the force of the Java be in all of us !!!
Joined: Jan 30, 2000
Adding a voice to the fugue: use the class name to refer to class variables and methods - don't use an instance variable name. The two forms are equivalent as far as the compiler and JVM are concerned, but using an instance to refer to a class property just creates needless opportunities for confusion. [ February 07, 2003: Message edited by: Jim Yingst ]