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.
First of all, do not start class names with small letters, because it's against convention.
You can just set the value in constructor, like that:
Have in mind that this way you will not be able to change this value anymore. For that you would need to define a proper setter:
and remove the "final" of field "env".
Don't hard coded, just keep the method taking any value, the specify the value you want out side the method.
Then:
But if you don't want to change the value, then don't create a setter method at all.
and then just read the value.
You can assign it directly in the attribute definition:
If the variable is always going to have the same value, declaring it as final is the best practice.
private final String env = "IN";
If the variable is not dependent on objects, declare it as static as well
private final String env = "IN";