I have taken to using setters instead of putting arguments into the default constructor because it helps me organise my code better
The problem is that the only variable on a project I am doing is a String and I am not sure whether I should be initializing it in the declaration (as a global variable?), in an setter instance method or whether to initialise it in the class constructor.
I am wondering if there could be anything problematic about this set up whether the instance is not initialised until it’s setter is used:
class MyClass{
private String myString;
public MyClass(){
}
public void setStuff(String s){
this.myString=s;
}
}
Not sure what you mean by global variable? Is it truely a variable that is the same inrespective of the object that is created? If so it should be static
If it never changes then it should be final and is then a global constant
This can then be public and accessed via
If it is only applicable for each object of the class that is created then either initialise it in the constructor or in the declaration