Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why are static variables seen as evil?

Status
Not open for further replies.

Soham1087

Banned
Joined
May 31, 2022
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
India
Activity points
185
I am a Java programmer in the corporate world. I recently created an application in Groovy and Java. Statics was utilized extensively throughout the code I created. The senior technical team urged me to reduce the amount of statics I utilized. I did some research and discovered that many programmers are opposed to utilizing static variables.

Static variables are more convenient for me to use. And I assume they're efficient as well (please correct me if I'm wrong), since if I had to make 10,000 calls to a function within a class as given in this example by scaler, I'd rather make the method static and use a simple Class.methodCall() on it rather than populating RAM with 10,000 instances of the class, correct?

Furthermore, statics eliminate interdependencies with other sections of the code. They can serve as ideal state bearers. Statics are also commonly implemented in other languages, such as Smalltalk and Scala. So, why is this anti-statics sentiment popular among programmers (particularly in the Java world)?

PS: Please inform me if my assumptions are incorrect.
 

The use of static variables in Java is not necessarily bad, but it can lead to certain issues that make code harder to maintain, test, and extend. Here are some reasons why some programmers are opposed to utilizing static variables:
  1. Global state: Static variables create global state, which means that their value is shared across all instances of a class and can be modified by any code that has access to them. This can make it harder to reason about the behavior of your code, especially when dealing with concurrency and multi-threading. When different threads access the same static variable, it can lead to race conditions and synchronization issues.
  2. Tight coupling: When you use static variables, you create tight coupling between different parts of your code. This can make it harder to test and change your code in the future, as any modifications to a static variable can have unintended consequences elsewhere in your codebase.
  3. Code reusability: When you use static variables, it can make your code less reusable, as you cannot easily swap out the implementation of a static method or variable for another. This can make it harder to write testable and maintainable code, especially when dealing with complex systems.
  4. Performance: While it is true that using static variables can improve performance in certain cases, it is not always the case. For example, if you are dealing with large amounts of data, it may be more efficient to use non-static methods and objects to avoid unnecessary memory overhead.
In general, it is best to use static variables sparingly and only when they are necessary. When possible, try to use dependency injection or other design patterns to achieve the same functionality without relying on static variables. This will make your code easier to test, maintain, and extend in the long run.

(Ref openAI)
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top