最佳实践在java中在实体类中设置一个变量值(硬代码) [英] Best Practice to set a variable value(Hard Code) in Entity Class in java

查看:625
本文介绍了最佳实践在java中在实体类中设置一个变量值(硬代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为所有对象设置一个字符串值(对于变量env)如下: -

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.

推荐答案

创建一个包含所有需要的字符串的常量文件。 Make
String public static和final。然后在您的代码中使用它

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.

这篇关于最佳实践在java中在实体类中设置一个变量值(硬代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆