Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of java static method calling

Tags:

java

oop

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 ?

like image 365
Naz Avatar asked Aug 11 '26 16:08

Naz


1 Answers

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.

like image 91
Elliott Frisch Avatar answered Aug 14 '26 07:08

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!