Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [static]

The static keyword, in object-oriented programming languages, is used to define a function/method or field/property as bound to the context of a type/class, but not to any specific instance. Unlike constants, static properties can usually be changed at runtime. static also refers to the storage class in languages like C. static variables are initialized once, at run-time, and persist in between function calls (i.e. they aren't re-initialized).

Filter by
Sorted by
Tagged with
34 votes
10 answers
17k views

Generating Even Random Numbers

I have this class which is used as part of a game. It needs to generate Random Even values, which is done by generating random numbers until the result is even. Is there a better way to do this? ...
leonideveloper's user avatar
13 votes
1 answer
13k views

Business with dbcontext and static class

Many speak of using dependency injection. I do not use repository. The entity framework is uow/repository. What is wrong with using static class like the one below? My context is a new instance per ...
Marcos's user avatar
  • 139
13 votes
3 answers
16k views

Unit testing legacy code with static classes

I am adding new functionality into a legacy application. For my new classes I have introduced Unit Testing. I still need to work with the existing code. One of the problems I face is that a lot of ...
Myrtle's user avatar
  • 528
13 votes
3 answers
25k views

Non static class with static fields

I have a class that listens to a file event constantly and reads that json file once it's created and makes of it a mail report and an excel file. Now, I made the properties that are used for both ...
Moshisho's user avatar
  • 357
12 votes
3 answers
8k views

Implementation of static_for to iterate over elements of std::tuple using C++17 features

I have implemented a static_for loop to iterate over the elements of an std::tuple using C++17 features. Here is what I have ...
Steve Rodeen's user avatar
12 votes
1 answer
3k views

Why does the new ADT create a static inner class Fragment by default?

Honestly, I can't help but feel that this is done merely to confuse newcomers. Most of the errors on Stack Overflow by complete Android newbies mostly stem from that they have a static inner class ...
Zhuinden's user avatar
  • 454
11 votes
5 answers
2k views

Static factory function and lifetime

I'm trying to teach myself C++ at the moment, after years of C# and other managed languages. The class in question is a Level in a game, and the idea is to ...
Michael Stum's user avatar
10 votes
2 answers
958 views

Basic Imitation of C# Enumerable in VBA - or any other static class

Since implementing static classes in VBA actually seems possible I came up with an idea to try to imitate C#' Enumerable class (well, to some extent...). This is ...
user avatar
8 votes
3 answers
4k views

Static utility class for string manipulation in C++

My programming foundations were formed in Java, but I've decided to only use C++ from now on for my projects as a personal learning experience. Static utility classes are common in Java; but I don't ...
leaustinwile's user avatar
7 votes
2 answers
5k views

Use of a static class for a game engine

I've been designing a game engine for the past few months with little issues. However, one main goal with this project is to put it on my portfolio/résumé so naturally I want the code to be as good ...
heyufool1's user avatar
7 votes
1 answer
11k views

C# - Make the static class thread safe [closed]

I have a simple class that I use to logging errors and other informations from my applications. I wondering what will happen when 2 or more threads of the same application would like to log something ...
Sebastian Xawery Wiśniowiecki's user avatar
7 votes
1 answer
3k views

Static variable in method vs private class property

I have a class with a method that will be called multiple times over an object's lifetime to perform some processing steps. This method operates on a mixture of immutable (does not change over the ...
Jon's user avatar
  • 588
7 votes
2 answers
9k views

Equivalent of C-style "Static Local Variable" in Swift

I'm porting some Obj-C code to Swift, and I've written the following code to allow me to deal with "static local variables" which do not exist in Swift. A static local variable has these ...
original_username's user avatar
7 votes
1 answer
2k views

Getting and setting a static variable

...
enon's user avatar
  • 387
6 votes
2 answers
887 views

Singleton design pattern

I am a beginner in PHP-OOP and design patterns. I have got this basic piece of code in Singleton Pattern and I changed it in order to understand its behavior. I can see that it works as expected. ...
DanielTheRocketMan's user avatar
6 votes
3 answers
2k views

Static methods or singleton?

I have class that handles HTTP requests: ...
Dmitry Zaytsev's user avatar
6 votes
2 answers
5k views

C# static class holding list of member class instances

I was wondering if such a design was a bad idea in C#. Here I have a static class EventLog which holds a list of instances of ...
Aske's user avatar
  • 63
6 votes
1 answer
4k views

Does this tkinter-based web browser widget use a well implemented class?

I have fully functional code in class format, and as far as I am aware from my previous question, it conforms to PEP 8 and the code logic and implementation is suitably pythonic. My main concerns here ...
anon582847382's user avatar
6 votes
2 answers
610 views

Handle static objects using custom adapter

I have read some articles discussing about why static is bad, some of them refering static as not-testable, non-mockable, has state (non-stateless) and everything else. However I have encountered ...
Fendy's user avatar
  • 173
6 votes
1 answer
950 views

Fake duck typing with dynamic

There are sometimes situations where you have a couple of related classes with static fields or properties but you cannot access them via ...
t3chb0t's user avatar
  • 44.3k
6 votes
1 answer
258 views

Use of static factory methods for vectors and matrices library

I've been working on a Java-based mathematics library focusing on vectors and matrices. I plan to use it for an important upcoming project, so the classes are analogous to data types available in GLSL ...
code_dredd's user avatar
6 votes
2 answers
2k views

Fill a vector with uniformly distributed random complex numbers

I want to fill an array of complex numbers using a uniform generator. I thought up the next code. (Complex is a simple fixed-point complex data type.) ...
JHBonarius's user avatar
5 votes
3 answers
263 views

Avoid static initializer for lists of image and video formats?

I wrote the following code to read constants from annotations: ...
gstackoverflow's user avatar
5 votes
4 answers
487 views

Configuring toString via a public static variable

Sometimes I need toString() to be quite verbose, normally I don't. It can't be nicely solved by using other methods as toString()...
maaartinus's user avatar
  • 13.5k
5 votes
1 answer
2k views

How to make static methods testable? [closed]

I am creating an application which will be testable(unit + integration). In this application I have a FileHelper static class, ...
Imran Qadir Baksh - Baloch's user avatar
5 votes
2 answers
2k views

MySQL connection using a static connection

I have been using a static class to connect and retrieve information from a MySQL database and also reading about how static works in php. I'm still not sure if the code is bad or not and why (besides ...
incompleteness's user avatar
5 votes
2 answers
111 views

Static utility functions to turn arrays into SQL statements

I have an object that houses only static functions. The functions are stand alone and are not dependent on the state of the object. I hear that static functions are a nightmare in terms of testing, ...
David's user avatar
  • 53
5 votes
1 answer
3k views

Javascript static and prototype mocking

I'm writing unit-tests for my Node.js/Express application with REST endpoints which retrieve stuff via Mongoose from db. Since I'm testing only route functions I want to mock Mongoose by providing ...
Tumetsu's user avatar
  • 153
5 votes
2 answers
1k views

Is it ok to add a static method to the IEqualityComparer implementation?

Linq methods like Distinct need IEqualityComparer object. But sometimes I just compares two objects and to create Comparer instance for that is seems redundant. So it would be nice to use static ...
SerG's user avatar
  • 171
5 votes
3 answers
2k views

`Static inheritance` pattern

I have an abstract class Entity in my code, which requires a reference to an instance of EntityData to operate. (...
Raburi's user avatar
  • 51
5 votes
2 answers
129 views

Calculating the number of elements of an underlying data structure

I read many comments on this question that one really shouldn't use local classes for functionality like which my code provides, but I couldn't really put things into perspective and decide what ...
MatrixAndrew's user avatar
4 votes
4 answers
279 views

Client to connect to server and show a text menu

First of all: Sorry for my English, that said... I am developing an obligatory for my University, so far it has a class that provides the user with options to connect to a server (once connected it ...
Alvaro Rodriguez Scelza's user avatar
4 votes
2 answers
140 views

Reflection and Project Euler

I have made a Project Euler runner in order to facilitate the running of the various problems. I have never used reflection before, and I believe that I have found a good use for reflection within ...
Brendan Lesniak's user avatar
4 votes
1 answer
2k views

Alternative std::function implementation for lambdas

I was trying to implement a faster, alternative implementation of std::function. I came up with the code below: ...
SomeProgrammer's user avatar
4 votes
2 answers
450 views

API for SQL queries with PHP PDO

I'm writing a very simple CRUD application, and I'm wondering if the way I'm using static methods throughout the code makes sense. I'd very much like to simplify ...
GingerBadger's user avatar
4 votes
2 answers
1k views

Translating Objective-C use of static and +(void)initialize to Swift

I am converting an old Objective-C class into Swift. My actual question is at the very end after all of the code. Here is a cut-down version of the Objective-C class: DateInfo.h: ...
rmaddy's user avatar
  • 195
4 votes
5 answers
970 views

Android app for sending username and password requests

I'm developing an Android app that performs several requests to a server using the AndroidAsynchronousHttpClient. One of these requests (as an example) is responsible to send the username and password....
Raphael Oliveira's user avatar
4 votes
1 answer
1k views

Primitive Extensions - Replaces static primitive methods

I got annoyed having to do things like string.IsNullOrEmpty(myString) when it seemed as if myString.IsNullOrEmpty() would ...
Michael Brandon Morris's user avatar
4 votes
1 answer
13k views

Python decorator to support static properties

Classes in Python do not have native support for static properties. A meta-class can rather easily add this support as shown below. Are there any problems programmers might experience if they use this ...
Noctis Skytower's user avatar
4 votes
1 answer
190 views

Model class for networking that uses notifications

I just finished a model class that represents an Instagram Networking client for my iOS app. I am specifically looking for a review of my use of static constants. These are strings that represent ...
user3344977's user avatar
4 votes
3 answers
2k views

Static class member destruction in C++

I have a basic cache set up. Whenever a user requests a bitmap, it fetches or loads it from disk if it isn't already loaded, significantly reducing load times. Currently, the design explicitly tells ...
Casey's user avatar
  • 748
4 votes
2 answers
162 views

Code organization when using threads

From OOP & OOD point of view, is it good idea to define Java-threads inside of the static method or in this case it's better to use instance-based method? ...
Mike's user avatar
  • 277
4 votes
1 answer
684 views

Getting enum values of Pseudo-Enum classes

By "Pseudo-Enum" classes I mean cases where someone's used a class with public static properties to emulate enum behaviour; e.g. as described here. Per the example below, this is useful for classes ...
JohnLBevan's user avatar
  • 1,409
4 votes
1 answer
78 views

Code for simplifying the creation of Dimension Objects

I have a class that I use to represent a distance called Dimension. Here is its four constructors: ...
jth41's user avatar
  • 253
4 votes
1 answer
363 views

A decent use-case for static property

I'm currently writing a REST-tool that connects to a third-party server (duh). They offer a fairly large (if not too large) API in a single wsdl file, as well as separate wsdl's for each specific part ...
Elias Van Ootegem's user avatar
4 votes
1 answer
790 views

Bank with factory method for accounts, multithreaded deposit and withdraw

I have a couple questions about this program Should I implement runnable instead of using thread class? I like the idea of the consumer and producer being thread objects, it seems simpler to me, but ...
James's user avatar
  • 43
4 votes
3 answers
337 views

Function decorator for static local variables

I'm primarily a C++ developer, but I find myself writing significant amounts of Python these days. One C++ feature I miss in Python are function-scope static variables (variables which are initialised ...
Angew is no longer proud of SO's user avatar
3 votes
2 answers
8k views

What's wrong with static utility classes, versus beans?

My inclination is to make these methods static: ...
Thufir's user avatar
  • 379
3 votes
4 answers
723 views

CSV parsers and OrderedDictionary

I wrote the following (static) class to read files easily in C#. However, it is a static class, so I wonder if there would be a gain to make it a non static method (and therefore, passing arguments as ...
RUser4512's user avatar
  • 1,033
3 votes
1 answer
1k views

Ensure only a single instance of static template class member

I am interacting with a C library, whose documentation states that certain functions are not thread safe. Since its functions deal heterogeneously with multiple types, I have written a template ...
wakjah's user avatar
  • 133