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
4
votes
1answer
83 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 ...
1
vote
2answers
70 views

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

Suppose I have 3 classes, Animal, Dog, and Cat ...
3
votes
1answer
38 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 ...
3
votes
1answer
51 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 ...
4
votes
3answers
68 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 ...
2
votes
1answer
46 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 ...
3
votes
1answer
75 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 }, ...
0
votes
1answer
634 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 ...
8
votes
3answers
584 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
183 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
101 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
48 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
2k 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
645 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
108 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
560 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
73 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
1k 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
6k 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
203 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
1k 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
6k 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
420 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
596 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
303 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
3k 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
330 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
2k 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
682 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
270 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
231 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
83 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
351 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
141 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
249 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
80 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
526 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
859 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
690 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
682 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
991 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
452 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
944 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
9k 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 ...
7
votes
1answer
7k 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 ...