简体   繁体   中英

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. How does java deals with this kind of issue ?

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. 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. Thus, it is best to write clear and concise code (that is Write Dumb Code ). If the clearest way to implement your solution is static methods then use them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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