简体   繁体   English

java静态方法调用的效率

[英]Efficiency of java static method calling

my professor said that when ever i use a static method from a class the whole class gets loaded into memory and then the method is executed. 我的教授说,当我从类中使用静态方法时,整个类被加载到内存中,然后执行该方法。

my question is: if a class contains 100 methods and 50 different variables and if i called one static method from that class.the complete class(100 methods and 50 variable ) gets loaded in memory which is inefficient in terms of memory and performance. 我的问题是:如果一个类包含100个方法和50个不同的变量,并且我从该类调用了一个静态方法。完整的类(100个方法和50个变量)被加载到内存中,这在内存和性能方面效率很低。 How does java deals with this kind of issue ? java如何处理这类问题?

True, the class byte-code is loaded when you call a static method (but once , not every time ).. The same also happens when you call a non-static method. 诚然,这个类的字节代码,当你调用一个加载的static方法(不是每次都只有一次 。)..当你调用一个非静态方法同样也会发生。 In the later case an instance also must be created. 在后一种情况下,还必须创建一个实例。 Thus, in the sense of your question, it is a false dichotomy. 因此,就你的问题而言,这是一种错误的二分法。 Because Java is a dynamic language and platform (with a JIT) the runtime efficiency can increase significantly between method invocations. 因为Java是一种动态语言和平台(使用JIT),所以在方法调用之间运行时效率会显着提高。 Thus, it is best to write clear and concise code (that is Write Dumb Code ). 因此,最好编写清晰简洁的代码(即Write Dumb Code )。 If the clearest way to implement your solution is static methods then use them. 如果实现解决方案的最清晰方法是static方法,那么使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM