GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 64 of 502    Print  
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.

  
Total Answers and Comments: 3 Last Update: July 25, 2009     Asked by: Beena 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sunil kulyana
 

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.



Above answer was rated as good by the following members:
talktoatish
September 26, 2005 10:02:37   #1  
Pankaj        

RE: It is a saying that static methods in JAVA program...

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


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 4Overall Rating: -4    
July 21, 2006 00:57:32   #2  
sunil kulyana        

RE: It is a saying that static methods in JAVA program...

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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 25, 2009 01:09:15   #3  
geeksterrahul Member Since: July 2009   Contribution: 9    

RE: 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.
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).

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape