0

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.

user2918640
  • 473
  • 1
  • 7
  • 25
  • 1
    https://codereview.stackexchange.com – Naman Aug 28 '17 at 11:20
  • 2
    Have you asked him? – Jacob G. Aug 28 '17 at 11:22
  • @Jacob I will ask him soon. But I want to know first if it is a bad practice or not? – user2918640 Aug 28 '17 at 11:24
  • 3
    It's hard to guess your colleague's intent based solely on this piece of code. Taken out of context it looks stupid, but he may well have had his reasons. Do not hesitate to ask him. – Maurice Perry Aug 28 '17 at 11:26
  • If he just wants to read the fields in the declaring class instance then I see no reason for making them static. Using final must be seen as a good use of Java convention in my opinion. – Jack Flamp Aug 28 '17 at 11:30
  • It's a unit test. He forgot. Or he remembered that there will only ever be one instance of it. If it was production code it would be wrong. – user207421 Aug 28 '17 at 11:49

1 Answers1

-1

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.

  • Somebody downvoted your answer. I agree with you that static should not be misused for the sake of memory leak. But against what you said static along with private & final is used to create application-wide constants when there are numerous object instances using them. – user2918640 Aug 28 '17 at 14:34
  • Yes I made a mistake there of course you can use static to create a constant but what i actually meant was that the keyword static doens´t make it a constant variable that´s the job of final. – Alexander Heim Aug 29 '17 at 07:15