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 ... |
Urgh, I'm kind of confused on how enums work in Java. In C# and C++ (what I use normally), this seems okay, but Java wants to get mad at me >.>
...
|
I tried to declare a class as shown below
class Outer{
private final class Inner{
public static final String s1 = new String("123");
...
|
Hey guys I have a quick question. I am reading from a file and setting values based on what I read from the file.
My question is: If I wanted ... |
This may seems a silly question for Java developers, however, I'm new to Java, and my background is from low level c.
I used to include an header file with all the ... |
I would say it depends where exactly are you trying to use that constant and how often it's (if ever) value would change. For example writing an email address in the web.xml would make more sense and not in a class file. Then in case you define a constant like PI it would make more sense to define it at the ... |
Believe it or not, I'm actually having trouble finding information on this. I would like to declare a constant within a method. For example, let's say that my "addRecord" method uses the field name "empleid" many times. Typically in other programming languages, I would declare a constant within the "addRecord" method because... a) I prefer sticking values in variables whenever it's ... |
|
I'm trying to set some values that remain constant and available throughout all classes in the package. I'm guessing I'll have to put them in a class, but will I have to refer to that class each time I reference them i.e. it would be nice to simply refer to ConstX instead of ConstClass.ConstX (yes, I come from a pampered Delphi ... |
More of a beginner's question. If it is constant, and intended for use by many instances, why do you want several copies? If the value of BOILING_POINT is always 100, why should each Water object need its own copy? They can share the static field and save memory. It also means that BOILING_POINT is available to other classes without having to ... |
Hi All, In one of my recent projects, I used an interface to declare constants like public static final int UNKNOWN_ERROR = 100. I have several such constants declared in interface and what ever class wants to use these constants can implement the interface. However, my lead was not happy with the approach, instead he suggested me to declare constants in ... |
|
If the constant is only "constant" for a particular build of the application but might change in other builds, use a configuration file (properties files are easiest). This is particularly important if you may need to make localizations to the "constant". Strings are often good candidates for moving out into configuration files. Use an enumeration if the exact value of the ... |
You can use enum if the set of cars vendors will not change. If car vendors is undefined you can use static final String (you can't use switch, but switch has not sense with String parameter) or you can define your own class with the functionality needed and a overriden toString() method that return the name of company. |
Hi, I have a question. I am in an introductory java course and my instructor says that anything that is declared outside of a method (ie. very beginning of a class) must be a final constant. She says that I must use public static final int EXAMPLE_CONSTANT; and that it can not be changed (I know this, it is declared as ... |
You should use enums instead of static int constants. There are many advantages to doing this - type safety, meaningful string representation, being able to iterate over all the constants in a group, being able to obtain the size of a group, etc. Additionally if you use static int's because they are compile-time constants they are compiled into the clients that ... |
|
|
|
|