• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Disadvantages of using static variables unnecessarily

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can anyone please tell the Disadvantages of using static variables.
Thank you,
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, the question doesn't make sense to me.
You use a static variable when the stored data is associated with a class, and a non-static one when it's associated with an object. That's it, isn't it?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By definition there is exactly one copy of a static variable so if two objects need to keep track of independent values, a static flat don't work. That's not so much a disadvantage as just how it works.
One real risk is that static variables are essentially like global variables in many other languages. If they are public then any object in the system can change them at any time. If many objects are changing and testing values all over the place, it can be very hard to control or understand how changes affect other objects. Testing, debugging, reproducing problems all become harder. Globals are counter to good OO encapsulation where data lives inside a protective object shell.
BTW: It's a little trickier than "exactly one copy" but that's enough to think about for a while.
Hope that helps!
 
Jagadish Gowda M N
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help. But i think there is a memory issue if a large number of static variables/methods are used. Because they remain in the memory since they will not be garbage collected till the main program ends.
Thank you Ilja Preuss and Stan James for taking time to help me. Have a nice day.
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jagadish:
Regarding the memory issue that you bring up in your previous post, because there is only one copy of a static variable for any given class, I am not sure why this would normally be a concern. Do you have specific case in mind?
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

does using most of the variables in application as static make the application slow ? I googled for the same but couldn't get the how this exactly impact the performance of application. does garbage collector not collect the static variables ?

Also my friend told me that we should avoid to use static instance variable unless it is very much mandatory. But in my case application this is desktop application and I need only one instance of the class so not creating object, just using static instance variables.

looking forward for some brief explanation if you could provide in practical scenario.

Thanks in Advance
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:static instance variable


Isn't that contradictory?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:I need only one instance of the class so not creating object, just using static instance variables.


Are you referring to singleton - sorry the statement is not clear. When you don't create any object, then there is no instance of the class. Please DontWakeTheZombies
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:
does using most of the variables in application as static make the application slow ?



No. Like many common questions beginners ask, the answer to this one is, "We don't choose X based on performance, but rather, based on design, and to make the code easy to write, read, test, debug, and enhance."

Do you understand what "static" means in terms of the Java language and how that relates to our design?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I made a mistake, by static instance variable i meant static variables.

John Jai wrote:

Manoj Kumar Jain wrote:I need only one instance of the class so not creating object, just using static instance variables.


Are you referring to singleton - sorry the statement is not clear. When you don't create any object, then there is no instance of the class. Please DontWakeTheZombies



I meant we are having classes those are having variables those are all static, in application we are not creating the object of the classes we just using the classname.variableName, we are also having methods most of them are static. we are not creating any object of class. We are using classes as to store the data.. I am new to this application so wondering if this is impacting its performance, is this some architecture problem
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:we are also having methods most of them are static.


If you're not creating any instances of the class, why do you have non-static methods ? You can't use them without creating an instance of the class.

Manoj Kumar Jain wrote:so wondering if this is impacting its performance, is this some architecture problem


Using static methods is unlikely to have any effect on performance. Whether your design has architecture problems is practically impossible to say without knowing full details of what your system does and what it's current architecture is. Do you not have any design documents that explain the reasons why the system was designed this way ?
 
Marshal
Posts: 79941
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We worry about zombies a lot less than we used to.

No, static members do not affect execution speed. They may be collected as garbage when the class they belong to is unloaded. If anybody thinks static has anything to do with garbage collection or performance, it means they have completely misunderstood what static means. The same sort of thing applies to static methods. There is a classification of methods produced by a very dubious character giving a hint when a method ought to be static.
Saying members should be static because you will only create one instance does not make sense. A static field can be regarded as shared between all instances (and other things, too), so it looks bad design to have one instance sharing its fields with the other instances, even if there are no other instances. If you have instances you have instance members. In fact, in an object-oriented language, you should make all members instance members as a default, and only make them static when they need to be static.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:
Do you understand what "static" means in terms of the Java language and how that relates to our design?



Yeah static means that this variable is related to class and not specific to any instance of the class. So this can be used with/without creating the object of the class and if we don't need class to show different behavior for different objects we use static variables, static variable are common for all objects of the class (if objects have been created)

But while googling for the same problem on many blogs/forums it was mentioned that static cause memory issue, but I am still not sure how it create this issue...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You say your class is for storing data. Is this data used throughout the lifetime of your system or is it just created, used briefly and then no longer needed ?
If the latter then you could be using up memory unnecessarily.

If you had something like this, then the ArrayList in StaticClass nor any of the data in it would ever be garbage collected. So you have a memory leak.

If you make everything in the class non static, then as soon as the ProcessData method completes, all that data is eligible for GC.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:

Jeff Verdegan wrote:
Do you understand what "static" means in terms of the Java language and how that relates to our design?



Yeah static means that this variable is related to class and not specific to any instance of the class.



So this can be used with/without creating the object of the class



While that's technically true, I don't like to look at it that way. It makes it sound like creating an instance is a bad thing, and using statics "lets" you avoid it. That's the wrong view to take when working in an OO paradigm.

Just stick with "associated with the class as a whole, rather than one particular instance." And here is where your logic breaks down, IMHO. You said, roughly, "Since I won't be creating multiple instances, I can just make everything static, because I don't need to distinguish among different instances."

You shouldn't be thinking, "I'm only ever going to use one, so I can get away with making it static." Rather, you should be thinking, "How will one of these things behave, and what state do I need to drive that behavior?" (That is, what variables and methods apply to a given instance of this class.) And you design and code it as if there could be multiple instances. If you happen to end up using just one in this particular case, that's fine, but you've still defined what "one of these things" is and what it does, and that's how you use it--as "one of these things."

But while googling for the same problem on many blogs/forums it was mentioned that static cause memory issue,



As stated, that's utter nonsense. If you can provide the exact citation, we can help you under stand what was really meant, or if the author was just off his rocker.

 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Joanne, your first StaticClass is exactly that, what we are doing, we are loading all the data in static variables (lots of data from file/database) that might be required in the application further. This is a Swing desktop application having around 10-15 screens and lots of functionality. so as soon as GUI is up we are having all data in memory irrespective of that whether it is being used or not.

I am getting a point now that actually using static variable is not the root cause of the problem, the reason is that we are keeping all the data in memory whether we required it now or not, and probably we are doing this because we don't required many instances of class and just using the class to solve our purpose and ignoring to create a instance.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

While that's technically true, I don't like to look at it that way. It makes it sound like creating an instance is a bad thing, and using statics "lets" you avoid it. That's the wrong view to take when working in an OO paradigm.


But while googling for the same problem on many blogs/forums it was mentioned that static cause memory issue,



As stated, that's utter nonsense. If you can provide the exact citation, we can help you under stand what was really meant, or if the author was just off his rocker.



Jeff, you are correct probably the idea behind using static was that this can be used with/without creating the object of the class.(in subconscious mind)
I think the creating an instance was avoided because it was not required.

however one of the link i followed was as below:

http://gbracha.blogspot.com/2008/02/cutting-out-static.html
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:
however one of the link i followed was as below:

http://gbracha.blogspot.com/2008/02/cutting-out-static.html



Okay, I skimmed that article, and was immediately put off by the extreme tone of it. There's a lot of rhetoric about the horrors of static variables there, but it read more to me like political fear mongering than a technical argument. Yes, when they overused they cause problems. But while they are prone to abuse and not strictly necessary in the language, they do have their uses.

As for "memory issues," there's not much of a practical problem there for Java programmers. The real problem you would see is if your static variable is referring to a huge object graph, but you're not using the class any more, so that the static variable is causing a lot of memory to be wasted. This only occurs if the class could be unloaded but for some reason isn't. Otherwise, it's either a non-issue, or else you're still using the class and, assuming you have a decent design, that static variable is part of the class, so you're still potentially using it too.

When all is said and done, "memory issues" for static variables are really no different than for non-static ones. You almost never have to think about it, other than to make sure you're not holding on to stuff you're not using any more.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:Yes Joanne, your first StaticClass is exactly that, what we are doing, we are loading all the data in static variables (lots of data from file/database) that might be required in the application further. This is a Swing desktop application having around 10-15 screens and lots of functionality. so as soon as GUI is up we are having all data in memory irrespective of that whether it is being used or not.

I am getting a point now that actually using static variable is not the root cause of the problem, the reason is that we are keeping all the data in memory whether we required it now or not, and probably we are doing this because we don't required many instances of class and just using the class to solve our purpose and ignoring to create a instance.


Well the usual reason for doing something like this is that fetching data from a database can be slow, so you read it once and then cache it in memory. This is a valid design approach but it does, as you have found out, use a lot of memory. At this point you have to decide where your priorities lie - fast access to data or low memory usage (of course a combination of the two is also valid).
If you are sure that it is this cache that is causing you memory problems, then the first thing you need to do is get rid of the cache and read everything from the database every time you need it. You then need to measure what affect this has on your system's performance. If the performance is acceptable, then your problem is solved - if it's not then you need to come up with a solution somewhere in the middle. One option is to restrict the size of your cache and then when you hit the limit dump the least recently used block of data.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jagadish Manoj - you might also want to read this.
It's always best to tell us what your actual problem is, rather than asking questions about something you think might be a possible solution. It would have saved everyone a lot of time.


Edit - I don't suppose Jagadish is all that interested now.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Jagadish Manoj - you might also want to read this.
It's always best to tell us what your actual problem is, rather than asking questions about something you think might be a possible solution. It would have saved everyone a lot of time.


Edit - I don't suppose Jagadish is all that interested now.



Joanne I am new to application and appilcation was quite slow and what I can see there is that most of the thing is static, so I was in inpression that it might be due to over use of static.

I always try to first search for myself, I then came to know that use of static might cause memeory leak and can affect performance but not sure about the explanation, and as I found a thread in Java Ranch for the same discussion(which didn't answer my question) and I WokeTheZombies....

Thanks all of you for your time and helping me..


 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic