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
2 votes
2 answers
73 views

Accept an array, erase some elements and print the array

I'm trying to learn about OOP, specifically encapsulation, and use of static. My goal is to improve my coding skills, I am just looking for feedback specific to encapsulation, the use of static, and ...
Alexander Dale's user avatar
1 vote
1 answer
65 views

Improving poor man's translations mechanism in c++98 program (follow-up)

As a follow-up to this question that was an attempt to add basic translation capabilities in an old c++98 program for the sake of the community, I'm posting here the first iteration of my code after ...
MatG's user avatar
  • 149
0 votes
1 answer
122 views

Improving poor man's translations mechanism in c++98 program

In order to add basic translation capabilities in an old c++98 program I've come up to a basic and shameless code summarized by this snippet: ...
MatG's user avatar
  • 149
2 votes
2 answers
714 views

PhoneBook in Java - follow-up

This is a follow-up to this phonebook code. This Phone Book allows the user to Enter a value between 1-3. Entering 1 lets the user Add a number to the phone book, when the process is finished, the ...
Grewal_Creator'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
1 vote
0 answers
26 views

Using (templated) function-scope static address as Unique ID [closed]

typedef const bool* const TypeID; template<typename> TypeID TypeIdOf() noexcept { static const bool idLoc(0); return &idLoc; } I'm using ...
InKryption's user avatar
4 votes
2 answers
449 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
1 vote
2 answers
2k views

Magic Static in Singleton Template

I recently was reading an article labeled "Modern C++ Singleton Template". The proposed solution used C++11 feature of Magic Statics (N2660). I wanted to adapt it for an own project in "...
Heinz's user avatar
  • 11
1 vote
1 answer
280 views

Json config loader class

Currently the class takes a json file and retrieves data from it, and i'd like to improve this class. ConfigLoader.h ...
Nadpher's user avatar
  • 889
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
1 vote
2 answers
87 views

Overriding static method with different static variable in java [closed]

Suppose I have 3 classes, Animal, Dog, and Cat ...
AlexPetrovich's user avatar
3 votes
1 answer
60 views

PHP get minDateTime from array

I've written a function to get the smallest date from an date[ ] and check for null. If null == infinity ...
CasualBen's user avatar
  • 133
3 votes
1 answer
183 views

Static Arrays and Variables (JavaScript)

I would like to know if this code of the database for my Pokemon clone game follows general practices in syntax and general readability. I feel like I could cut down on my comment usage, and place ...
Osman Mammedov's user avatar
4 votes
3 answers
335 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
2 votes
1 answer
291 views

PHP - using static variables to store "client" for later use

I had this S3Helper class before such that, every time I called a function to any of its methods (which I haven't shown here, but which do things like upload files to s3 or get temporary s3 links), it ...
TKoL's user avatar
  • 229
3 votes
1 answer
109 views

Caching lookup values in an e-commerce website

I am working on an e-commerce website. I have a CarMake dropdown which contains car make names and their corresponding Id, for example: { 1:Alfa romeo }, { 2:Audi },...
Hooman Bahreini'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
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
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
0 votes
1 answer
90 views

Generically call static methods

I currently have two (core) classes : ...
WynDiesel's user avatar
  • 145
0 votes
1 answer
618 views

Usage of static block vs. direct static variable initialization [closed]

Suppose, I have a data access layer class, where I want to specify database connection parameters. Usually, I do it as static variables and initialize it right after definition, e.g. see ...
Mike's user avatar
  • 277
0 votes
1 answer
66 views

Logging util with static lambda function

So again, I am a bit confused about some notes that i'am getting from my reviewer. I have some kind of utils, to print/prepare log messages. Yea, maybe I am a bit fanatic java 8 lambda guy but I think ...
shutdown -h now's user avatar
1 vote
1 answer
3k views

Static wrapper class for log4net Logging - thread safety

I'm using log4net in my application and have a static Log class which creates an instance of ...
DGibbs's user avatar
  • 153
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
2 votes
0 answers
40 views

Multiple CResourceUsers using a single CResource

I need multiple CResourceUsers to share one instance of CResource. I want to instantiate the ...
Teni's user avatar
  • 21
0 votes
2 answers
273 views

Read employee number and salary (exercise to use static keyword)

WAP to create a class called Employee having empid and salary as data members and salary as data members. Create objects of this class and count no. of employees working in an organization using ...
Ravi Singh Lodhi's user avatar
3 votes
2 answers
855 views

Web browser in Swing with MVC

Follow up question to: Utilizing OOP when creating a web browser After I received help on the question above, I've created 3 classes for my Browser. ...
armara's user avatar
  • 279
1 vote
2 answers
198 views

Manipulating an ES6 static class property that is an array

I've been trying to find a way to create static properties in ES6 classes and have found a solution with the help of various Stack Overflow answers (namely this one and this one). However, I also ...
ReeseyCup's user avatar
2 votes
2 answers
2k views

Creating lists of Card objects

I need to create two lists of Card objects in C#. A Card class is defined as: ...
Yu Zhang's user avatar
  • 707
2 votes
2 answers
9k views

Using Autofac to do DI in a static factory method

I'm using Autofac to create one of my classes inside of a static factory method Create. This code is working and I haven't noticed any side effects yet. Admittedly, ...
t3chb0t's user avatar
  • 44.3k
1 vote
2 answers
229 views

Creating Immutable Student Object

For a project I'm working on I have the following requirements: We have two student types: Domestic and International ...
user avatar
3 votes
1 answer
2k views

Static helper function to set a property on another object

A situation has come up in which we have a block of code that will be repeated in multiple files, and this block of code has to: Create some object based on the values of other controls/properties. ...
peter's user avatar
  • 133
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
3 votes
1 answer
912 views

Instance registry for keeping track of class instances

In my project, I need to be able to iterate through all living instances of a given class. I ran into a problem regarding static initialization/deinitization order where when an instance attempts to ...
gsemac's user avatar
  • 131
0 votes
1 answer
1k views

C# API Wrapper with Static Function Calls

I'm creating a wrapper library for an API, and I hit a block on how to redesign it. The entire library is static, and I'd like to change it to make it more testable. The library's goal is to make ...
user avatar
2 votes
0 answers
498 views

Threadsafe static initialization of ExecutorService

I am trying to create a threadsafe singleton class based on Initialization-on-demand holder idiom . Here is my code ...
Steve's user avatar
  • 21
2 votes
2 answers
5k views

SQL connection in static class and wrapper

I need to make some calls to stored procedures in my SQL database from a .NET application, using C#. I have something like this, and I put just one of the methods but the rest will be similar: ...
mitomed's user avatar
  • 539
1 vote
1 answer
81 views

Improving Design for structured Keys

On several occasions I had to use a fixed set of keys (constants in the sense of static final String) with some additional logic (grouping in different keySets). After some more or less lefthanded ...
Krümel Monster's user avatar
1 vote
1 answer
3k views

Enum of calculator operations

I'm making my first simple project which I decided would be a calculator and I'm kind of stuck on implementing the operations. I have two classes one of which is the GUI part and the other one ...
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
1 vote
0 answers
763 views

Mocking protocol with class/static functions in Swift

I am using one of the open-source Objective-C keychain wrappers to store data in iOS keychain. For test cases, I have created protocol as: ...
D4ttatraya's user avatar
-1 votes
2 answers
293 views

Initializing a big static array for a card-based iOS app

I have a card-based iOS app that uses a sizable static array of cards. Each card has several properties, mostly strings, numbers and boolean. The first version of this app had 3-4 properties. The ...
Zonker.in.Geneva's user avatar
2 votes
1 answer
298 views

A simple Dice Game Using only Static methods

This is a simple dice game that requires you to get two numbers from the user and then it generates 15 other numbers and checks if the generated numbers are the same as the numbers typed in. Try to ...
user avatar
2 votes
1 answer
96 views

Server side of a chat program - part 2

A few days ago I posted a question about my code. It was obviously awful and thanks to some advice from Hosch250 I learnt a lot of things about OOP. Now, I rewrote the program. To me it seems a ...
TomCa's user avatar
  • 113
4 votes
1 answer
680 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
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
-2 votes
1 answer
103 views

Updating contacts in mobile phone simulation

I'm currently making a mobile phone simulation, using a custom object in an ArrayList. I was wondering if there was any difference in using these two following ...
PacketSniffer's user avatar
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
1 vote
1 answer
1k views

Unit test Web API controller that uses a static helper class which uses app config setting

I have a situation where I have a static class that reads my application configuration file for some configuration value. When unit testing Web API controllers that make use of this static class I am ...
Sykomaniac's user avatar
3 votes
1 answer
1k views

Extending functionality of org.springframework.batch.item.file.transform.DefaultFieldSet

I would like to be able to set token values (defaultFieldSet.tokens) and names (defaultFieldSet.names) on ...
James's user avatar
  • 299