Answer the question
In order to leave comments, you need to log in
Resetting all attributes of a static class to null?
For example, a class of this kind, which serves as a kind of analogue of a stretch in c ++, there should be 15-20 attributes in total. With an extensive switchcase that will fill in certain attributes of this class. And in order not to constantly reset each of the attributes to 0: attr1 = null, attr2 = null, attr 3 = null .... attr20 = 0, then I thought, in a friend in Java, there is something similar to resetting a memory block as in C .
public static class Test{
public Long attr1;
public String attr2;
public Double attr3;
public Long attr4;
private final static Test instanceTest = new Data();
private Test(){}
}
Answer the question
In order to leave comments, you need to log in
Null is generally bad. Why do you need a static class if you will change the variables in it anyway, maybe it's better to look towards singletons? Why reset variables to null at all?
It is better to write a specific place and the task you are facing so it will be much easier to say how best to solve the problem.
I'm assuming you would like this:
public class Test{
public static Test instance;
public Long attr1 = null;
public String attr2= null;
public Double attr3= null;
public Long attr4= null;
private Test(){}
public static synchronized Test getInstance(Boolean clear){
if(instance==null || clear==true) instance = new Test();
return instance;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question