Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 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).

0
votes
1answer
41 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 ...
0
votes
1answer
44 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 ...
1
vote
1answer
793 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 ...
6
votes
1answer
331 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 ...
2
votes
0answers
36 views

Multiple CResourceUsers using a single CResource

I need multiple CResourceUsers to share one instance of CResource. I want to instantiate the ...
0
votes
2answers
94 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 ...
3
votes
2answers
217 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. ...
1
vote
2answers
63 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 ...
1
vote
2answers
716 views

Creating lists of Card objects

I need to create two lists of Card objects in C#. A Card class is defined as: ...
2
votes
2answers
3k 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, ...
1
vote
2answers
181 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 ...
3
votes
1answer
657 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. ...
11
votes
3answers
3k 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 ...
3
votes
1answer
228 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 ...
0
votes
1answer
356 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 ...
2
votes
0answers
212 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 ...
2
votes
2answers
2k 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: ...
1
vote
0answers
269 views

Utility classes for XML

I have a class which makes calls to a static class. this class started off as a test utility class, primarily for performing operations on xml via XPath. Eventually some of the methods in the XmlUtil ...
1
vote
1answer
76 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 ...
1
vote
1answer
991 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 ...
6
votes
2answers
892 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.) ...
1
vote
0answers
528 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: ...
-1
votes
2answers
207 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 ...
2
votes
1answer
207 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 ...
2
votes
1answer
82 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 ...
4
votes
1answer
261 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 ...
1
vote
0answers
130 views

Android Internal Resource Reader

I'm not really sure if this is right, so it would be great to have it reviewed. I'm creating an Android library and made a final class with completely static methods for getting internal resources (...
4
votes
4answers
246 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 ...
-2
votes
1answer
72 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 ...
3
votes
4answers
399 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 ...
1
vote
1answer
671 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 ...
3
votes
1answer
509 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 ...
4
votes
1answer
419 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 ...
2
votes
2answers
774 views

Current user as a class-level property in Objective-C and Swift

I was recently tasked with architecting a user/profile object in Objective-C and wanted the ability to access a static instance from the class level, similar in style to the way Parse manages their ...
1
vote
1answer
173 views

Implementing builder pattern In Java with a static inner and a Concrete standalone class

I am studying Gang Of Four design patterns and I want to implement a builder pattern in Java. It seems there are two options to build a builder pattern: Using two different classes: A target class ...
2
votes
2answers
228 views

Remove all occurrences of a digit from a number

I need to remove all occurrences of a digit from a number in a recursive function with this prototype: unsigned rem(unsigned num, unsigned dig); This is my ...
5
votes
2answers
911 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 ...
11
votes
1answer
6k 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 ...
6
votes
1answer
3k 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 ...
5
votes
2answers
94 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, ...
4
votes
2answers
95 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 ...
1
vote
1answer
57 views

Class and Static practice, IDS and a max instance count [closed]

I was practicing working with classes and the static property included with them and came up with a simple ID counter and max number list. I also added a quick debug error ...
0
votes
1answer
108 views

YUIDoc syntax for optionally static constructor

In JavaScript, I wrote a class that can be optionally instantiated with the new keyword, but can also be called statically as well. What is the best way to ...
4
votes
1answer
2k 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 ...
2
votes
2answers
825 views

config.properties, Static Classes and Reflection

I wanted an opinion about an idea I had regarding the use of config.properties, static classes and the use of reflection. I have to load some preferences at application startup, for that I will be ...
7
votes
2answers
7k 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 ...
6
votes
1answer
192 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 ...
5
votes
3answers
249 views

Avoid static initializer for lists of image and video formats?

I wrote the following code to read constants from annotations: ...
5
votes
2answers
4k 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 ...
5
votes
2answers
4k 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 ...