Questions tagged [static]
66 questions
24
votes
4 answers
Make methods that do not depend on instance fields, static?
Recently I started programming in Groovy for a integration testing framework, for a Java project. I use Intellij IDEA with Groovy plug-in and I am surprised to see as a warning for all the methods that are non-static and do not depend on any…

Random42
- 10,370
- 10
- 48
- 65
22
votes
5 answers
Is the use of ***Helper or *** Util classes containing just static methods an AntiPattern
I am often confrontated with helper or util classes in Java or whatever kind of language.
So I was asking myself if this is some kind of Anti Pattern and the existence of these kind of classes is just a lack of missings in the design and…

Diversity
- 329
- 1
- 2
- 7
16
votes
3 answers
Java: Is it okay to abuse Spring beans (@Component) rather than use static final utility classes?
I'm torn between using DI and static utility classes on my code.
I currently created a DateUtil static class that is only meant to be accessed statically. This class is responsible for just creating and manipulating dates using a variety of…

mpmp
- 781
- 3
- 8
- 12
16
votes
1 answer
Are we abusing static methods?
A couple of months ago I started working in a new project, and when going through the code it stroke me the amount of static methods used. Not only utility methods as collectionToCsvString(Collection elements), but also plenty of business logic…

user3748908
- 1,607
- 2
- 14
- 15
15
votes
4 answers
Writing 'interfaces' for static classes
I'm writing a postcode validation library, so that I can call a helper method
var result = Postcode.IsValid(postcode, country)
To that end I need to have 'classes' that represent supported countries and know how to validate each. At present I have…

Quango
- 271
- 1
- 2
- 8
13
votes
1 answer
static globals and anonymous namespaces in C++
Why did C++ make any distinction between static globals (internal linkage) and symbols in an unnamed namespace (external linkage, but no way to refer to it from outside anyway), when introducing the latter?
Are any of those reasons still valid, or…

Deduplicator
- 8,591
- 5
- 31
- 50
12
votes
2 answers
How isolated are static variables?
If I have a
public class SomeClass {
public static final HashMap hashmap = new HashMap();
}
and then I have five different classes with main(String[] args) methods, that I will run.
when they access SomeClass.hashmap, would they be accessing…

ycomp
- 267
- 3
- 8
10
votes
3 answers
Best approach for multilingual Java Enum
I'm running into issues with an approach I am taking and am now wondering if I just started down the wrong path and should rethink my approach. Here is what I attempting.
I would like to use an Enum to represent different possible Account Statuses…

forevernewb123
- 101
- 1
- 1
- 5
8
votes
4 answers
Are static global variables as bad as global variables? How to avoid using them?
In general, I know that global variables are bad and should be avoided.
Are static global variables equally bad?
In all my projects, I have heavily relied on static global variables. From design perspective, how can I avoid using them.
Ex Use case…

Sandeep
- 179
- 1
- 6
8
votes
4 answers
Static vs. non-static? (with non-OOP functions)
Although this question has come up before, mine is different because it is not supposed to cover anything OOP but only plain (ANSI) C.
Especially in open-source software, I usually encounter a plethora of functions (please don't call them methods…

syntaxerror
- 189
- 1
- 1
- 5
8
votes
6 answers
Significant amount of the time, I can't think of a reason to have an object instead of a static class. Do objects have more benefits than I think?
I understand the concept of an object, and as a Java programmer I feel the OO paradigm comes rather naturally to me in practice.
However recently I found myself thinking:
Wait a second, what are actually the practical benefits of using an object…

Aviv Cohn
- 21,190
- 31
- 118
- 178
6
votes
2 answers
Static console object or dependency injection?
For a current project I have created a Console class which wraps up C++'s usual cout, writes output to a file or uses Console::WriteLine depending on the environment. Currently I've setup my code base to inject a console object into each class that…

sydan
- 373
- 1
- 4
- 11
5
votes
3 answers
Etymology of "static" functions
I get why static local variables are called "static" -- we want them to be allocated in static memory! But what is the reason for calling functions and variables we want restricted to the current file "static"? I don't see the connection; either…

Elliot Gorokhovsky
- 607
- 6
- 13
5
votes
3 answers
Creating a Java project without using any objects and just calling static methods on input?
I'm creating a program which takes a document, extracts it, and then moves this into a database. I've done most of this now and I've realised that I haven't made any instances of classes I've made (I have made instances of other thing such as…

Dan Savage
- 59
- 3
5
votes
3 answers
Why static methods can't call non-static methods directly?
I don't understand why most programming language don't allow developers/users to call non-static method from static method directly? What is the reason behind it?
p.s. I know, you can create object and call non-static method from static method.

Abhishek
- 169
- 1
- 2
- 9