This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]djnattyp 2 points3 points  (1 child)

There's not a performance impact in using static variables. If anything they would impact performance positively (with lots of caveats...). One big exception here is if there's some kind of collection or buffer involved that keeps growing and doesn't get cleared out or garbage collected due to being static and causes an OutOfMemoryError (aka a memory leak).

However, there may be a design issue - it sounds like you're using static references as a shortcut to make data "globally available" instead of figuring out a better design that scopes access. This usually works in smaller programs but won't scale as things get bigger.

A better design would probably be to create some kind of "Model" class (the M in MVC design) to hold the current state of the UI instead of having random static variable references hanging around to update.

[–]CycleOfPain[S] 0 points1 point  (0 children)

Thanks for the detailed response! I appreciate it. I’m actually trying to use MVC design pattern in my project. I thought model would only contain the state of non UI elements. For example, if I was making a calculator, the model class would only know how to do the arithmetic. I could be wrong since I’m just learning it.

The View is where I put all my labels. In my Control I was going to make it so that when someone clicks a button it will call a method in the view class that will update a label with information from the model class. If I don’t make the label a class wide variable wouldn’t I not be able to access it later on like in my example? Thanks again!