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
0
votes
1answer
62 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 ...
7
votes
3answers
110 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 ...
4
votes
2answers
127 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: ...
0
votes
1answer
79 views

Generically call static methods

I currently have two (core) classes : ...
0
votes
1answer
46 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
45 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
1k 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
502 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
38 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
96 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
317 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
67 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 ...
2
votes
2answers
919 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
4k 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
191 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
841 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
4k 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
314 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
425 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
244 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
296 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
1k 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
1k 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
603 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
250 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
215 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
289 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
137 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
75 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
436 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
771 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
569 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
520 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
840 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
258 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
477 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
1k 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 ...
12
votes
1answer
7k 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
4k 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
95 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
98 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
110 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
874 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
8k 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 ...