This question is bound to scream poor programming practice, however, I’m curious if there is any performance risk involved here.
Imagine you have a class that only has one method attached (excluding the constructor) to it, for simplicity sake we’ll say:
public class TestClass{
public TestClass(){
// Set values or whatever you want in the constructor
}
public String printString(){
System.out.println("print");
}
}
Now considering there is only one method, obviously anytime you use the class you’ll probably want to call the method printString. So are there any negatives (besides sanity) to putting a call to printString in the constructor? Rather than doing testClass test = new testClass() then making a call test.printString()?
Again, this question is about performance – not programming practice.
what you can do is use Enum
There will not be much difference in performance point of view but from coding point of view you will have no need to create object every time. Also you can have static utility class as enum.