Multiple constants in same class - java

I want to know what is the best way to use final constants in the same class? since i have to use a lot for the same class and looks very meshy, thanks.
How i can make it look more readable, since if I need to store more variables there will be many........... thanks
public class Default {
private static final String CLASS_FILE_EXTENSION = ".class";
private static final String JAVA_PACKAGE = "java.";
private static final String NO_PREFIX = "";
private static final String SUFFIX = "sfx";
private static final String TEMP_SUFFIX = "tmp";
private static final String NO_PARAMETERS = "()";
private static final String STRING_DESCRIPTOR = "Ljava/lang/String;";
private static final String RETURNS_VOID = "V";
private static final String JAR = "jar";
private static final int ONLY = 0;
private static final int BUFFER_SIZE = 1024;
private static final int PACKAGE_LOOKUP = 0x8;
private static final int BASE_VERSION = 44;
private static final Class<?> NOT_FOUND = null;
private static final URL NOT_SEALED = null;
private final TypeDescription instrumentedType;
private final TypeAttributeAppender typeAttributeAppender;
private final AsmVisitorWrapper asmVisitorWrapper;
private final ClassFileVersion classFileVersion;
private final AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy;
private final AnnotationValueFilter.Factory annotationValueFilterFactory;
private final AnnotationRetention annotationRetention;
private final Implementation.Context.Factory implementationContextFactory;
private final MethodGraph.Compiler methodGraphCompiler;
private final TypeValidation typeValidation;
private final ClassWriterStrategy classWriterStrategy;
private final LatentMatcher<? super MethodDescription> ignoredMethods;
private final List<DynamicType> auxiliaryTypes;
private final ClassFileLocator classFileLocator;
public Default(..... etc)

There is no objective answer possible for this. A few points to consider:
Having a lot of constants in one class might mean that the class is doing a lot of work. See if you can split the class into multiple classes and declare the constants as per the usage within those classes.
Since all these are private fields, see if they are really being used more than once in the same class. If not you can directly use the string rather than having a constant for that. The downside is, in the future, if you want to use it again in the same class at a different place, you have to remember to make it a constant (which is very easy to forget).
(I don't prefer this) I have seen cases where people will have an interface just for holding constants and static import them into the class to use it. That interface is like a big dumping ground for the constants.

Related

Would setting a field in a class as a new instance of the same class cause memory overflow? InetValidator class (apache.commons)

I am using InetValidator class from apache.commons in my project to validate IP addresses. While going through the source code,
public class InetAddressValidator implements Serializable {
private static final int IPV4_MAX_OCTET_VALUE = 255;
private static final int MAX_UNSIGNED_SHORT = 65535;
private static final int BASE_16 = 16;
private static final long serialVersionUID = -919201640201914789L;
private static final String IPV4_REGEX = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
private static final int IPV6_MAX_HEX_GROUPS = 8;
private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP = 4;
private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
private final RegexValidator ipv4Validator = new RegexValidator("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$");
public InetAddressValidator() {
}
public static InetAddressValidator getInstance() {
return VALIDATOR;
}
I observed there is a VALIDATOR field which is calling the constructor again.
What I'm confused with is if I create a new instance of this class as,
InetAddressValidator addressValidator = new InetAddressValidator();
won't it will recursively keep creating new instances of the same class (as the field VALIDATOR creates a new instance everytime) and eventually I'm gonna run out of heap space?
I haven't tested this yet, but wondering if this is something I have to keep in mind while testing.
private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
it's a static field, so it will be instantiated just once at class load time, not per-instance in the constructor.
Recursive call
InetAddressValidator addressValidator = new InetAddressValidator(); Recursive call

Correct way to use static parameters in Android

I am pretty new in Android and I do not know how is the proper way to manage static constants. I mean, I need to use several constants (such as COMMAND_BACK = 100) in several Java classes and activities. It is not beautiful to declare them as attributes in each single activity so, what is the correct way to do this?
I though about declaring them in strings.xml, but it does not seem suitable neither...
Thanks in advance.
You can make a class like this :
public final class AppConstants {
//put all the constant here
// Eg :
public static final int SPLASH_TIME = 1000;
}
The disadvantage by declaring it in a resource.xml file is that you need a context to receive the value. This is fine as long as you need those values inside a context class otherwise you have to pass one around.
The elegant solution would be extending the Application class since the android os itself uses static fields that way.
Declare
public final class ConstantClass {
public final static int COMMAND_BACK = 100;
}
Usage
int num = ConstantClass.COMMAND_BACK;
Add a Constants class to the project
public class Constants {
public static final String STRING1 = "First String";
public static final String STRING2 = "Second String";
public static final int INTEGER1 = 1;
public static final float FLOAT1 = 0.1f;
}
// Use
textView.setText(Constants.STRING1);
Create a common interface where you can declare all the constants.Constants can further be grouped here to make it mode clean.
public interface Constants {
public interface XYZ{
public static final int A= 1;
public static final int B= 2;
}
public interface REPORT_TYPE_FLAGS{
public static final String C= "0";
public static final String D= "1";
}
}
Another elegant way is to define a constant class with other inner subclasses
`private final class Constant {
public static class TypeOne {
public static final String NAME = "Type 1";
public static final int CODE = 1;
}
public static class TypeTwo {
public static final String NAME = "Type 2";
public static final int CODE = 2;
}
}
`
And you can access it in this way
`String typeOneName = Constant.TypeOne.NAME;
int typeTwoCode = Constant.TypeTwo.CODE;
`

How to replace string group with a string in eclipse

I have a set of constant like this:
private static final transient String NOME_FILE_RC = "blabla1.xml";
private static final transient String NOME_FILE_NS = "blabla2.xml";
private static final transient String NOME_FILE_NE = "blabla3.xml";
private static final transient String NOME_FILE_DT = "blabla4.xml";
private static final transient String NOME_FILE_MC = "blabla5.xml";
private static final transient String NOME_FILE_NR = "blabla6.xml";
I want to replace FILE with DOCUMENT in eclipse
I set find with \w{4}_(FILE)_\w{2}
I know that $1 catches FILE string but I don't know how to replace it with DOCUMENT.
Use (\w{4}_)FILE(_\w{2}) instead and replace with $1DOCUMENT$2.
capture the rest of the string
(.*?)FILE(.*)
and replace with
$1DOCUMENT$2

What are the possible ways to store this data?

Well, I need this data to be used in at least two classes:
String navHome = "home";
String navAbout = "about";
String navUpload = "upload";
String navUploadProc = "uploadProc";
String navStartUpload = "startUpload";
String navContact = "contact";
String navSearch = "search";
String navManual = "manual";
String navBackToGmis = "backToGmis";
I was thinking of just pasting that into other classes, but then it violates the principles of programming, since it's repeating the same thing. Text and XML files are not comfortable to store data, inheritance isn't an option, since Java doesn't support multiple inheritance... Any ideas?
Yeah, I know it will sound retarded to most of you, but I need tipps on this.
If these are constants, then you can declare them in one class (or interface) and use them in another:
A.java:
public class A {
public static final String navHome = "home";
public static final String navAbout = "about";
public static final String navUpload = "upload";
public static final String navUploadProc = "uploadProc";
public static final String navStartUpload = "startUpload";
public static final String navContact = "contact";
public static final String navSearch = "search";
public static final String navManual = "manual";
public static final String navBackToGmis = "backToGmis";
. . .
}
B.java:
import static A.*; // or list each String in a separate import
public class B {
. . . // code can use nav* as if they were declared in class B
}

Using #SuppressWarnings("unused") annotation on declaration section

I have a basic question relating to the #SuppressWarnings("unused") annotation. Basically, I would like to suppress unused warnings on a block of code in my declarations section of my class. I would prefer not to suppress unused warnings on the entire class.
Currently, I am doing this:
public class MyClass {
//Constants
private final String ACCEPT = "Yes";
#SuppressWarnings("unused")
private final String DENY = "No"; //Not currently used
private final String TENTATIVE = "Maybe";
#SuppressWarnings("unused")
private final String POSTPONE = "Later"; //Not currently used
//Variables
private int counter;
....
I find the above code to be a bit messy with the annotations thrown in randomly like that. What I would like to do is something like:
//Constants
#SuppressWarnings("unused")
{
private final String ACCEPT = "Yes";
private final String DENY = "No"; //Not currently used
private final String TENTATIVE = "Maybe";
private final String POSTPONE = "Later"; //Not currently used
}
//Variables
private int counter;
....
This is, of course, illegal in Java as a block cannot be simply made like this. Its not a huge deal or anything, but I am curious if I am missing an obvious and elegant solution.
If you reeeeealy want to keep all those unused constants, a quick and dirty trick would be;
public class MyClass {
//Variables
private int counter;
#SuppressWarnings("unused")
private static final class UnusedConstants{
private final String ACCEPT = "Yes";
private final String DENY = "No";
private final String TENTATIVE = "Maybe";
private final String POSTPONE = "Later";
}
}
And i would make them static too, but your call.
EDIT:
Or, what i think is much nicer:
public class MyClass {
private enum Answer {
ACCEPT ("Yes"),
DENY ("No"),
TENTATIVE("Maybe"),
POSTPONE("Later");
private final String answer;
private Answer(String answer) {
this.answer = answer;
}
public String toString() {
return answer;
}
}
}
This way you don't get the unused warning as long as you use at least one of the values.

Categories

Resources