Search code examples
javaclassentity

Best Practice to set a variable value(Hard Code) in Entity Class in java


i am trying to set a string value(for variable env) as follows for all the objects:-

public class example{
private String env;
public String getEnv() {
        return env;
    }
public void setEnv(String env) {
        this.env = "IN";
    }
}

is it the right way to Hard code the variable value or any better way to do it.

Thanks.


Solution

  • Create a constants file which contains all the Strings necessary. Make the String public static and final. Then use it in your code

    Example: this.env =  ConstantsFile.IN; 
    
    // tomorrow you can change its value to 
    "pinnnn" only in the Constants file and not worry 
    about changing every other filed that uses "IN" (now "pinnnn" :) ).
    

    This keeps your code clean and reduces dependency.