I have four questions regarding static methods in java:
1) When static methods are loaded in memory? when class loaded or when method get called first time?
2) When static method is unloaded from memory? When class unloaded or when execution flow of that method is ended after call?
3) In which memory area of JVM static methods are loaded? In stack memory area or there is different kind of memory available in JVM for static methods(I have read about some static memory also)?
4) Is it good to have so many static methods in our application?
I have gone through so many questions over this site and over other sites, but they don't clearly specify it.
1) When static methods are loaded in memory? when class loaded or when method get called called first time?
When that particular class loaded by the class loader.
2) When static method is unloaded from memory? When class unloaded or when execution flow of that method is ended after call?
When JVM garbage collected that particular class loader which loaded the static class.
3) In which memory area of JVM static methods are loaded? In stack memory area or there is different kind of memory available in JVM for static methods(I have read about some static memory also)?
Typically in Pergemen space.
where is a static method and a static variable stored in java. In heap or in stack memory
4) Is it good to have so many static methods in our application?
Yes, when they are solving the purpose individually.
1) When static methods are loaded in memory? when class loaded or when method get called called first time?
ans) when class loaded
2) When static method is unloaded from memory? When class unloaded or when execution flow of that method is ended after call?
ans) Static methods gets loaded when class is loaded by ClassLoader, and would be removed when it is Unloaded
3)In which memory area of JVM static methods are loaded? In stack memory area or there is different kind of memory available in JVM for static methods(I have read about some static memory also)?
ans)Inside a Java virtual machine instance, information about loaded types is stored in a logical area of memory called the method area. When the Java virtual machine loads a type, it uses a class loader to locate the appropriate class file. The class loader reads in the class file--a linear stream of binary data--and passes it to the virtual machine. The virtual machine extracts information about the type from the binary data and stores the information in the method area. Memory for class (static) variables declared in the class is also taken from the method area.
4)Is it good to have so many static methods in our application?
ans)One rule-of-thumb: ask yourself "does it make sense to call this method, even if no Obj has been constructed yet?" If so, it should definitely be static.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With