Questions tagged [static]
The static tag has no usage guidance.
67
questions
1
vote
1
answer
441
views
Should I use private field or static variable in my method?
There is a variable that must persist between calls but otherwise only one method uses it.
As it is a method of the object, the value can be persisted as a private field of the object, or otherwise it ...
0
votes
6
answers
462
views
Is a static class full of static methods a code smell?
I have to create custom buttons but in order to encapsulate the logic of button creation I've this class (VB.net):
Public Class ButtonCreator
Public Shared Function CreateBaseButton(Optional ...
0
votes
1
answer
207
views
Observer pattern using static events
I'm working on a game prototype: I have a Cell class which, given a mouse click on its collider, can raise an event that should be listened in two different modules: Camera and UI.
The Observer ...
0
votes
1
answer
246
views
Why exactly was the C++ convenience of not requiring a second declaration, removed?
See "Note" on second page:
This convenience gave rise to serious inconsistencies and was removed several years ago
The book was authored in 2003.
What exactly does it mean by "...
0
votes
2
answers
401
views
Best practice for storing a static pdf file in a web app
I've to store a static pdf with an user guide of my application.
I'm using Spring boot and Angular 12, what is the best way to store it?
Put the file on resource folder of Spring boot?
Put the file ...
1
vote
2
answers
2k
views
Dependency Injection vs Static Dependencies
I am building a wrapper for a library that requires little complex logic. The whole project is 8 builder classes, 4 classes for doing some pre-processing, and a couple visitor classes. Essentially I ...
3
votes
5
answers
2k
views
How do Singletons differ from Static variables?
Although I do find some (apparently old) posts on the topic on the web, I could not find one here at SE. Thought of raising this here to see if what I read is accurate/is all there is to it.
So ...
3
votes
2
answers
759
views
File scope static variables with non-trivial destructors
I am coming from the Google C++ guidelines, specifically the section on Static and Global variables. It advises against having global variables of types that aren't trivially destructible. I'm unsure ...
1
vote
4
answers
839
views
How to convince colleagues to stop writing static classes [closed]
I recently joined a new company. After working on some projects I noticed a pattern. Every project contains a lot of static classes. A lot of static Helper classes. ApiHelper JsonHelper UmbracoHelper ...
3
votes
3
answers
750
views
Is there any performance hit associated by the definition of a static constructor or due to availability of it?
This question is mostly related to the way language implementer do implements static class initalization (pretty specialized question). It is for curiosity and also to potentially improve my code to ...
2
votes
2
answers
971
views
C99: Alternative to "static" variable declaration?
By coincidence I stumbled over "The New C Standard" from Derek M. Jones, where he comments this sentence from the C99 standard regarding "storage duration of objects":
There are ...
1
vote
1
answer
181
views
Identifying Risks/Gotchas When Using Static
I am intending to come from the perspective of development choices, code reviews, and general testing against defined environments (Development, Test, Production, etc.). I will be using C# as the ...
2
votes
1
answer
1k
views
Storing static data idea — is it best practice?
I have a database in which I am storing a bunch of companies that can have several different types of codes associated with them — here is a example of a group of codes
I was wondering if storing them ...
-2
votes
1
answer
388
views
What is the best way to store secure static data which can be easily updated by clients?
I'm currently building an education website which contains a lot of content such as questions, videos and notes which all need to be securely locked away and exposed to users with relevant permissions....
4
votes
2
answers
2k
views
what to use instead of finalize() in java
Let's consider following code:
class Table {
private static int number_of_Tables=0;
public Table(){
++number_of_Tables;
}
public void finalize(){
--number_of_Tables;
}
public static int ...
-2
votes
2
answers
393
views
C# Static & Constructor Logic [closed]
I learn C# and try to understand the logic between static and Constructor right now. One thing I need ask you about an example which I will give at below. (please ignore the quality of code or how ...
3
votes
1
answer
1k
views
Alternative to a utility class for extension methods in C#?
I understand that in OOP languages like C#, it is generally viewed as bad practice to create utility classes, and it's preferable to put methods in the classes in which they will be used.
To that ...
-1
votes
2
answers
211
views
Object composition in a static function
I have a class that provides basic functionality of a modal window. For different situations I need to provide different modals(different number of buttons, titles, etc...). I have a kind of factory ...
0
votes
1
answer
191
views
Is it a good idea to make huge objects dynamic in C++? [closed]
I have an engine and if I want to use the 3d system I always have to pass a pointer to any mesh renderer. If I would make the 3d system static then I just could use them. But the 3d system is big with ...
22
votes
5
answers
14k
views
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 ...
2
votes
4
answers
1k
views
Use of static methods to implement logic within a class
I've recently caught an odd behaviour in my own coding: At some point in the last year I began creating protected static methods to implement the logic of my public methods, ensuring that the ...
-1
votes
1
answer
116
views
How to Model below Hiearchy with OOP
Note that I cannot use static inheritance due to language limitations (Java).
There is a general Building class. Each instance of Building has properties that exist regardless of instance variables (...
-1
votes
2
answers
4k
views
C#, Static classes and Inheritance
As a beginner I'll try to explain my problem as good as I can:
I'm currently trying to program a "simple" ECS. My basic idea is that I have a base "Entity class" which includes all sorts of functions ...
16
votes
3
answers
14k
views
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 ...
15
votes
4
answers
35k
views
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 ...
2
votes
1
answer
517
views
Is usage of Nested classes an example of tight coupling ? (JavaFX)
So I'm working on this game where I have a nested class which has to get access to the member functions and variables of the outer class in order to perform it's functionalities. In particular the ...
0
votes
1
answer
1k
views
Access static constant via interface or abstract class in java without instantiating
Need an advice on how to achieve decoupling of UI graphs from content.
Preamble:
I have an abstract Sensor interface and a bunch of implementations. Interface defines a couple of functions, such as ...
2
votes
2
answers
546
views
What is the proper (or preferred way) to configure a static factory class?
First SE stack,
I am working on a large chunk of old Java code and am finding tons of duplication and inconsistently configured/created objects because of different authors, skill levels, etc.
I ...
0
votes
2
answers
4k
views
Static vs non-static in embedded systems [duplicate]
I am writing a C++ applications in the embedded area. A lot of times I think about should I use static or non-static const variable in the functions/methods? Here are my thoughts:
Static constant ...
1
vote
3
answers
3k
views
ASP.net MVC static class and background worker
I have an MVC 5 ASP.net app that is working perfectly. A colleague of mine has made a project that reads in data from files and churns some of it to output some calculations for me to display in the ...
1
vote
2
answers
231
views
What is a best design to hold a “global” mutable list?
Let's say that I have an ArrayList of class Person and I have two objects that are generated from two different classes which read and write to this ArrayList.
For example,
public class Main {
...
0
votes
2
answers
752
views
Does anonymous object instantiation have advantages over static object creation in this specific case?
In a Java program which I did not made and cannot change, only write extensions for it, the design forces me to create an object which will never be garbage collected. I tested and I can do it through ...
2
votes
3
answers
6k
views
Static Method in Interface
I'm working on software to play back log files. We have different types of logs from different providers.
I've created an interface that different classes will implement to play different logs. For ...
4
votes
2
answers
741
views
When to use a static method instead of a constructor?
I have a short question for you: let's imagine that we have a class similar to this one.
public class StreamTradeDataProvider : ITradeDataProvider
{
public StreamTradeDataProvider(Stream stream)
...
3
votes
3
answers
978
views
Should only static functions print to screen?
My question is more about trying to figure out if my belief is correct or valid in that a static method should be the only one that prints to the screen (let's say in a terminal). I am using Java and ...
5
votes
3
answers
405
views
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 ...
-2
votes
1
answer
372
views
How does accessing class level variables in java work?
I'm doing my third online course in learning java because I couldn't get over how to even start a project from scratch. There was a lesson in this new course that made me ask this question of the code ...
2
votes
1
answer
6k
views
Collect and prcess data from multiple threads in Java
There is a Java program that reads a list of orders and for each order starts a thread that receives data from SQL and prints the list of items in a given order to an order_name.html. I want to write ...
8
votes
4
answers
5k
views
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 ...
0
votes
3
answers
451
views
Java constructors confusion? [closed]
public class example
{
private String one;
private String two;
public example(String one, String two)
{
this.one = one;
this.two = two;
}
public static void ...
1
vote
1
answer
1k
views
Manual reload of Java classes static block
I have a static block which fetches data from database. This data is then being used by instance method. Since static block will be loaded once I fear that the data won't change in its lifetime.
How ...
16
votes
1
answer
9k
views
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&...
1
vote
2
answers
7k
views
Moving Java static variables into XML
I've been working on a small Java project by myself for a department that does not do a lot of software development but mostly database stuff.
I showed my boss the code I've been writing and he saw ...
0
votes
2
answers
265
views
Can there be value types in dynamic languages? [closed]
A value type is one whose instances are themselves saved in variables. A reference type is a type whose instances are saved somewhere and variables only hold the addresses of the instances.
Some ...
12
votes
2
answers
5k
views
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 ...
13
votes
1
answer
9k
views
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 ...
0
votes
1
answer
258
views
Is it good practice to assign an instantiated class to a static property in the same class?
I have code that was written by someone else that goes like this:
On a ViewModel:
public static ACertainViewModel Default { get; private set; }
On the same ViewModel's constructor:
public ...
0
votes
1
answer
430
views
If everything is supposed to be an object in Java (barring primative types), isn't this contradicting the point of static? [duplicate]
I often write code which makes an object and then use static methods in static classes to manipulate said object.
Am I missing the point of OOP? How do I know whether I'm thinking in the OOP mind ...
1
vote
3
answers
866
views
Should class with only static data need to be instantiated?
I have two classes. 1. Node 2. MixedStateManager
Node class:
Node class maintains the state of nodes in the system. One such data is state.
public class Node
{
private int state;
...
5
votes
3
answers
2k
views
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 ...