We do use util functions and some functionalities like Logger,EventWriter,Some Common DB calls,etc often in our code. I prefer these functions to be static, because instantiating the functions from these classes in every one of my code would be a serious performance hit(would it be ?!!!? , I've read in stackoverflow that too much of class instantiantions would be a performance hit, I'm working on a Project with big Customer Database and high access log on the server). And I have came across static import in java
which looks cool, and I want to know: is there any Serious Considerations there before using it?
Things i've gathered already from StackOverFlow :
Using static import could make the Code Non-Readable, like judging the function definition.
Other than that , any Pretty issues which i have to worry about ..?
Old Code :
class myservlet extends httpServlet
{
pubilc doPost()
{
Utils utils = new Utils();
DBFuncs dbFuncs = new dbFuncs();
Logger logger = new Logger();
EventWrtr eventWrtr = new EventWriter();
utils.GetEscapedName(name);
logger.Log("error");
eventWrtr.WriteEvent("something happened");
// Getting new Objects for every servlet calls
}
}
My Current Code : ( Hope this will avoid unnecessary Instantiations, the code was like above, i'm changing it now like this)
/* Declaring all the methods in the below classes as static methods */
import com.mycom.Utils;
import com.mycom.DBFuncs;
import com.mycom.Logger;
import com.mycom.EventWrtr;
class myservlet extends httpServlet
{
public doPost()
{
Utils.GetEscapedName(name);
Logger.Log("error");
EventWrtr.WriteEvent("something happened");
}
}
I Kinda like this and i want to know any Serious Issues especially performance related in using the below approach
/* Declaring all the methods in the below classes as static methods */
import static com.mycom.Utils;
import static com.mycom.DBFuncs;
import static com.mycom.Logger;
import static com.mycom.EventWrtr;
class myservlet extends httpServlet
{
public doPost()
{
GetEscapedName(name);
Log("error");
WriteEvent("something happened");
}
}
The static import
feature is a syntactic sugar kind of feature, so it cannot have performance implications. It does have negative consequences to readability and testability, though:
static
objects, making it extremely hard to test. For example, you cannot easily drop a mock logger, and expect your code start using it. This is a general limitation of using static objects, though - you get it when you use static objects, with or without the static import.The answer to your question is:
No.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With