java static 性能,使用java:import static时的任何性能问题

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:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值