I was reviewing some code written by a colleague who is my senior. In a unit test class, he made use of private final
class variables like this:
public class SomeProcessingServiceUT {
private final String modelNumber = "ABC01";
private final String serialNumber = "000002";
private final String PrimaryId = "15033520583";
private final String CheckinTime ="20021010:00-05";
.....
}
And all he was doing was accessing all of these variables like this.modelNumber
etc and nothing else.
I want to question his intent as to why he did not declare pure constants by using static and why he used such mixed namings. But I thought of sharing my doubts here also with the community. This is a bit confusing. Any wisdom will be helpful.
The keyword static doesn´t make it a constant, it´s used for variables that are attached on the class and not the object. To say it simple: The value of the variable is the same for each object you create. The thing is that you can change the value of this variable if you say classname.variable therefore it´s not a constant variable.
In general you want to avoid using static, it´s not neccesarily bad programming but if you don´t absolutley need it shouldn´t be used. Also there is hardly ever situation where you need a static variable and in most cases could have been avoided with a better class design.
To answer your question, no it wouldn´t make much sense to declare that variable as static but I could be wrong here as I don´t know what else he wants to do with the class in general.