Questions tagged [static]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
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 ...
h22's user avatar
  • 917
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 ...
Mateo Ezequiel Bertogliati's user avatar
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 ...
vandermies's user avatar
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 "...
Anon's user avatar
  • 3,575
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 ...
Accollativo's user avatar
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 ...
EspressoCoder's user avatar
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 ...
Veverke's user avatar
  • 393
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 ...
Zoso's user avatar
  • 219
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 ...
Roy Berris's user avatar
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 ...
Eric Ouellet's user avatar
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 ...
mic's user avatar
  • 137
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 ...
eparham7861's user avatar
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 ...
Jamie's user avatar
  • 139
-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....
Ryan_DS's user avatar
  • 107
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 ...
user12394078's user avatar
-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 ...
Melih Cinar's user avatar
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 ...
Tara's user avatar
  • 151
-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 ...
7368616d696c's user avatar
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 ...
Kerbo Games's user avatar
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 ...
Diversity's user avatar
  • 329
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 ...
JohnLBevan's user avatar
-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 (...
Mario Ishac's user avatar
-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 ...
Maxracer's user avatar
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 ...
mpmp's user avatar
  • 781
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 ...
Quango's user avatar
  • 271
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 ...
johndoe123's user avatar
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 ...
stiebrs's user avatar
  • 113
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 ...
finleyarcher's user avatar
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 ...
VP.'s user avatar
  • 139
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 ...
Simon Nicholls's user avatar
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 { ...
CrazySynthax's user avatar
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 ...
user5950's user avatar
  • 113
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 ...
001's user avatar
  • 273
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) ...
masm64's user avatar
  • 349
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 ...
Alexis Evelyn's user avatar
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 ...
Elliot Gorokhovsky's user avatar
-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 ...
Rafael Cortes's user avatar
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 ...
sixtytrees's user avatar
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 ...
Sandeep's user avatar
  • 179
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 ...
gordon sung's user avatar
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 ...
sd1517's user avatar
  • 151
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&...
user3748908's user avatar
  • 1,607
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 ...
Wabbage's user avatar
  • 69
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 ...
Aviv Cohn's user avatar
  • 21.2k
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 ...
ycomp's user avatar
  • 267
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 ...
Deduplicator's user avatar
  • 8,601
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 ...
Tyress's user avatar
  • 153
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 ...
Dan Savage's user avatar
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; ...
Praveen Hassan's user avatar
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 ...
Dan Savage's user avatar