It is a saying that static methods in JAVA programs should be minimised. What is the reason for this? any problems arise or there is any performance measures to be looked into it.

Questions by Beena   answers by Beena

Showing Answers 1 - 3 of 3 Answers

Pankaj

  • Sep 26th, 2005
 

Static Method shld be minimised in Java Programm.For Ex . if i am m accessing any method and request modified the object then when other request comes it wll met to modfied Object.So we shld minimise the Static method in Java program as Original (Initial) value may be lost.

Pankaj

  Was this answer useful?  Yes

sunil kulyana

  • Jul 21st, 2006
 

Static variables and classes
In Java, usually a class member (variable or method) is accessed in conjunction with an object of its class. In the case of static variables and methods, it is possible to use a class member without creating an instance of its class. A class with static members is known as a static class. In such cases, before a class instance is created, an object of its class will also be created by the JVM. The class object is allocated to the heap itself. The primordial class loader will load the class object. In the case of static classes, all the static members will also be instantiated along with the class object. Once the variable is initialized with data (typically an object), the variable remains in memory as long as the class that defines it stays in memory. If the primordial class loader loads class instances, they will stay in memory for the duration of the program and are not eligible for garbage collection. So static classes and associated static variables will never be garbage collected. Thus, using too many static variables leads to memory leaks.

Static variables and classes
In Java, usually a class member (variable or method) is accessed in conjunction with an object of its class. In the case of static variables and methods, it is possible to use a class member without creating an instance of its class. A class with static members is known as a static class. In such cases, before a class instance is created, an object of its class will also be created by the JVM. The class object is allocated to the heap itself. The primordial class loader will load the class object. In the case of static classes, all the static members will also be instantiated along with the class object. Once the variable is initialized with data (typically an object), the variable remains in memory as long as the class that defines it stays in memory. If the primordial class loader loads class instances, they will stay in memory for the duration of the program and are not eligible for garbage collection. So static classes and associated static variables will never be garbage collected. Thus, using too many static variables leads to memory leaks.

One simple answer:


Static(per class) members are created one instance/class loader/class instance. so they will reside until the class unloads(application closes).

Meaning,they will stay during the lifetime of the application on the heap and may become a burden to the heap(too many objects o the heap-->memory leaks).

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions