Question

I'm new to Java and have the following question:
Is there an easier way of making methods for each variable?

The meaning behind the question is:
Do I have to define a method that does the exact same thing as other methods except that they use different variable names and types?

I think the most common methods with the same problem are the get- and set-accessors:
They share the same structure, the only difference are the variable types and names.

In my following example, you need to know:

  1. Variables varA-varD just represent the existance of multiple variables with various types and names.
  2. The variable "inheritor" is special. I do NOT implement inheritance, but I DO have to verify that somewhere, maybe in the inheritor of the inheritor of the inheritor the same variable
    • has a value ("!= null"; in case of Lists, HashMaps, Enumerations, etc.)
      or
    • has a value other than -2 (because 0 means nothing and -1 indicates "infinite" in my system, so I thought using -2 for indicating that the variable hasn't been set yet is a good idea; in case of Integers, Floats, Doubles, etc.).
  3. I have verification methods...
    • ...to check whether the variables have already been set (or not)
      and for this reason the code is located
    • ...outside of the setter because I have to check the variables even when they have not been set yet.
public class ExampleClass {

    private int varA;
    private String varB;
    private ExampleEnum varC;
    private List<OtherClass> varD;
    //there are more variables here...

    private ExampleClass inheritor; 

    public int getVarA() {
        return varA;
    }

    public void setVarA(int varA) {
        this.varA = varA;
    }

    public boolean validateVarA() {
        //-2 is "not set" for Integers
        if (varA == -2 && inheritor != null) { 
            return inheritor.getVarA() != -2;
        } else {
            return varA != -2;
        }
    }

    //Do I have to define three methods for each variable?
    //What if I had like 20 variables?!?
    //I would need 60 methods altough every third one
    //shares the same structure.

}

I needed some sort of "building plan" for a method:

public T getVar() {
    return var;
}

public void setVar(T var) {
    this.var = var;
}

public boolean verifyVar() {
    //How would I get the invalid value for each type?
    T invalidValue = ?;
    if (var == invalidValue && inheritor != null) { 
        return inheritor.getVar() != invalidValue;
    } else {
        return var != invalidValue;
    }
}

In the example above:

  • "Var" or "var" would be the variable name
    and
  • "T" would be the type of var
  • I have no idea how I would get the invalid value...

Sorry in case I think too complicated and there is a simple answer to my question. Furthermore, I apologize for any grammar mistakes that may occur.

Was it helpful?

Solution

For generic getters and setters, there's always Map<String, Object>, but I'm pretty sure that's not what you want, so you should stick to the JavaBean conventions (as mentioned in the comments, any IDE would generate those for you and it makes total sense to have them according to OOP recommendations).

Any attempt to implement generic accessors would sooner or later become some java.util.Map with tones of reflection around it. If that's what you want, perhaps you should reconsider your model and switch your type-safe beans to some free-form types like map.

For validation, there's the javax.validation package (JSR-303).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top