Any Performance Issue in using java : import stati

2019-06-19 17:28发布

问题:

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");           
    }
}

回答1:

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:

  • You dump the content of a potentially large class into your current namespace, making your code harder to read; modern IDEs mitigate this by letting you navigate to the definition of the object by clicking its name in a program.
  • Your code relies upon 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.


回答2:

The answer to your question is:

No.



回答3:

no it will not make any performance issue. but its better to use normal import Please don’t use this feature, because over a period you may not understand which static method or static attribute belongs to which class inside the java program. The program may become unreadable.