Stack Exchange Network

Stack Exchange network consists of 174 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

Questions tagged [static-methods]

The tag has no usage guidance.

4
votes
4answers
224 views

Is it a good idea to put in a bunch of static functions that are related to each other in terms of scope inside a class?

The language is PHP. I have several functions that relate to each other (and sometimes use each other) that I've decided to couple under the same class. Here are my reasonings, do keep in mind that I'...
0
votes
1answer
132 views

Why isn't there a static initializer in Python?

The equivalent of static { // Code here } does not exist in Python by default. The closest thing, while being "Pythonic", seems to be to create a decorator and do the initialization there such ...
3
votes
1answer
88 views

Should I replace a constant with static methods, if that constant usually 'cooperate' with a specific operator?

For example, to convert between g and kg, I have a constant 1000: public static final float G_TO_KG=1000; . . . this.result = someResult*1000; I found G_TO_KG always bind to operator '*'. So my ...
1
vote
2answers
446 views

Calling a static method from constructor's member initializer list

I'm implementing a simple rendering system for a game engine. In my engine I have renderable entities that have a Model component (I'm using inheritance as opposed to a ECS for my engine for now, but ...
9
votes
6answers
677 views

Is separating most classes into DTO and helper classes (if possible) a good or an anti-pattern?

For example, a class usually have class members and methods, eg: public class Cat{ private String name; private int weight; private Image image; public void printInfo(){ ...
3
votes
4answers
739 views

Public static method calls private constructor

I'm working in a C# codebase that was largely written by a former developer and this pattern is used extensively... public class AuditInserter { public static void Insert( DataContext ...
5
votes
2answers
245 views

Using static factory methods to prevent unwanted input

Many years ago, I posted a problem I had with some code and received a well written detailed answer that suggested the use of factory methods. I liked this approach, because I can provide a method ...
8
votes
5answers
377 views

Should methods that are not “pure functions” and that interact with external APIs or hardware be static?

When reading about when to make a method static or not, I've seen a general principle, as summarized by this post, that a method should only be static if it does not modify a state and its result ...
0
votes
0answers
23 views

How to unit test an SDL based frontend? Is it worth it? [duplicate]

I am currently beginning to create a small program/game in my free time and decided to try out SDL2 for my frontend/graphics code. I use the bindings from SDL2-CS, which is API-wise very close to the ...
0
votes
1answer
407 views

Should I avoid long method chaining simply to get a variable? [duplicate]

I am working on a game, and I have many instances where I have to do something like this: Game.getInstance().getEnergyDropManager().getDrops() Or: this.dimensionSelector.selectDimension(Game....
0
votes
2answers
82 views

Inside a non-static non-virtual method, if it just use a few non-static members, should I turn it into static methods with parameters?

According to Are there guidelines on how many parameters a function should accept?, it is ok for a method to have few parameters. According to https://stackoverflow.com/questions/11240178/what-is-...
0
votes
0answers
48 views

What's the value in marking static methods in a non-static class [duplicate]

I had a recent code review that gave me some slightly surprising feedback. It was in an instantiable service class that had a number of fully encapsulated private methods. In other words, methods that ...
1
vote
1answer
537 views

Singleton pattern without static

I heard that static (in the Java sense, basically a static method is called on the class itself and not on an instance) is not True OOP. However, how would the Singleton pattern be implemented in such ...
6
votes
2answers
286 views

If a method in class A needs extra dependency but not every client uses it, should I move it out of A?

For example, suppose I have a class like that: public class PlayerInfo{ public loadDataFromDB(){ //some code about DB framework } public string name; public int age; //...
3
votes
3answers
4k views

Is it safe to return a new object from static method?

Is it OK to have a class something like this public class Weapon { private string name; private int might; // etc... private Weapon(String name, int might) { this.name = name;...
7
votes
2answers
7k views

Dependency Injection vs Static Methods

I had an interesting discussion today with another developer about how to approach a class with a method that accepts a string and outputs string. Imagine something like the following which is ...
2
votes
3answers
880 views

Using static to create an immutable object

I was reading on this software engineering page about the use of static methods. However, I'm confused, and maybe it's about the context in which it talks about static. The idea is, that static is ...
1
vote
2answers
535 views

Why Named Constructors are getting popular shouldn't be an antipattern?

According to following article Named Constructors The Author suggests using static factory pattern to construct objects is way better than instantinate with new keyword. At the begining the idea is ...
0
votes
1answer
89 views

Is there a better way than StringHelper?

I want to have functions that convert numeric values into strings in English words, to pluralize nouns, to inflect, break by words and so on. I will use these throughout a big project. Normally I ...
-1
votes
2answers
463 views

In Go when to write a function with or without a receiver?

We're trying to write Go in the most possibile idiomatic way, but sometimes we struggle to find which is the best way. For example in our service we're creating/converting a struct from another one: ...
-3
votes
2answers
118 views

Implementing functional/reusable code [closed]

Writing code, in my opinion, usually involves 2 kinds of code: logical and functional code. While the logical part of the code always differs between every app and its goals, often the functional ...
0
votes
2answers
1k views

Static methods and testability

I have an application service which returns entity by its registration number public Entity FindByRegNumber(string number) { if (!RegNumber.IsValid(number)) { return null; } ...
4
votes
2answers
226 views

When to use a static method instead of a constructor?

I have a short question for you: let's imagine that we have a class similar to this one. public class StreamTradeDataProvider : ITradeDataProvider { public StreamTradeDataProvider(Stream stream) ...
3
votes
1answer
607 views

Handle Named constructors with factory pattern

In my current project I'm refactoring the code to get a DBAL. I have a class Entity that is the base class for all classes that model a database table. So there are several classes that inherit from ...
5
votes
2answers
5k views

Unit testing utility classes

All of us have some utility classes, which contain only static methods, for usage from different sources. Now, there can be two approaches which can be taken towards testing this piece of code. ...
6
votes
2answers
138 views

Deterministic statics vs inject-able classes

I have in mind several deterministic functions that I'd like to put together, but I'm struggling with the full impllications of doing: Static class with static methods Instance of a class that can be ...
13
votes
5answers
2k views

Why shouldn't static methods be able to be overrideable?

In answers to this question, the general consensus was that static methods are not meant to be overridden (and thus static functions in C# cannot be virtual or abstract). This is not only the case in ...
4
votes
3answers
419 views

What problems might arise if I didn't make a method static when I could?

I have a stateless method that takes an input, and based on that input returns an output. This method has no state so in theory it could be made static. But let's say I don't do this. What problems ...
0
votes
1answer
130 views

Using static methods in layered architecture

In layered architecture does it matter where static methods go? Or is it the architect deciding this? For example can the DAL be static to cache records and perform smart data retrieval at the ...
10
votes
4answers
8k views

Why would passing objects through static methods be advantageous?

Why would there be an advantage to use a static method and pass the reference to an object as a parameter rather than calling the method on an object? To clarify what I mean, consider the following ...
11
votes
1answer
3k views

Are we abusing static methods?

A couple of months ago I started working in a new project, and when going through the code it stroke me the amount of static methods used. Not only utility methods as collectionToCsvString(Collection&...
-2
votes
4answers
225 views

Why do we add instance methods to classes? [closed]

Rewrite of the Question: Is there a technical reason why we are not using static methods instead of instance methods. Technical reasons are for example: Performance or added type-safety. I reason ...
8
votes
5answers
7k views

Does making a method static save memory on a class you'll have many instances of?

In response to Aaronaught's response to the question at: Can't I just use all static methods? Isn't less memory used for a static method? I am under the impression that each object instance carries ...
8
votes
1answer
395 views

Is a large static initializer a code smell?

I am extending SimpleExpandableListAdapter in Android. I don't think Android's adapter is implemented very well, in that its constructors have a large number of rather complicated arguments and it has ...
3
votes
2answers
396 views

Create a globally visible method for an API

I'm trying to create a Java API that I will use in other projects. I understand that if I create new classes I can make objects in the other projects that have those classes. But what I want right ...
1
vote
1answer
110 views

When should I use static functions and when should I use non-static ones? [closed]

Sometimes, when I'm programming, I have to decide between adding a static method to a class that accepts an instance (or more) of that class or adding a non-static method. Here's an example: Static ...
1
vote
3answers
414 views

Dependency inversion without static methods?

It's clear that the dependency inversion principle and the use of interfaces makes software components less coupled and promotes maintainability. On the other hand, these principles make using static ...
1
vote
2answers
817 views

Using static classes to define methods for handling POCO class objects

I am pretty new to the OOP paradigm and for this current project of mine, I need to design and develop a web application backend in .NET MVC using C#. A friend of mine told me that the class objects ...
3
votes
1answer
632 views

Static properties and implicit “self” property in structures and enumerations vs classes in Swift

I am currently reading the Swift language documentation and came across these sentences in the chapter about methods: Similarly, type methods on structures and enumerations can access static ...
-1
votes
4answers
320 views

Is static going to make any difference in this code snippet

I have been to an interview and was asked this question - is there any difference adding or removing the static keyword in these classes? I know what static means but my understanding of this point ...
5
votes
1answer
1k views

How can I use the “Non-Member Functions Improve Encapsulation” pattern from C#?

In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function: http://www.drdobbs.com/cpp/how-non-member-functions-improve-...
3
votes
2answers
171 views

Can a class method be accessed both in an instance and statically?

I am relatively new to class design and I have a task that I'm not sure how best to complete, or whether my idea in general is a code smell. I'm developing a RPG where people can own monsters, so ...
1
vote
2answers
1k views

Is using multiple static classes with maximum one public method is a good idea

I'm writing a structural detailing (CAD) software for concrete buildings in C#. I have defined like hundreds of static classes each with one public method and if needed some private methods. Each one ...
1
vote
2answers
875 views

Programming against interfaces in Java

Supposing I have an interface Foo and a given implementation FooImpl. public class FooImpl implements Foo If I want to define a new operation on this class that depends on the particular ...
0
votes
1answer
207 views

Static and not-static: programmer quantum theory

Let me illustrate using the PHP language. The discussion here is, how should I do exactly to solve this problem in a clear and unambiguous mode. Imagine that I have a class called Path. This class is ...
0
votes
3answers
7k views

Why overriding a static method does not result in polymorphism in Java

Many say that we cannot override a static methods. But we can override a static method. The question is, when we override a static method why it does not result in polymorphism ?
1
vote
1answer
784 views

Static methods vs Interface implementation

Here is my problem: I stared to create an e-commerce web site info collector. So I created a parser for each site. The parser class is stateless. I have got methods like: getItemPrice(WebElement ...
0
votes
3answers
3k views

Drawback of using static method [duplicate]

For methods that never access instance variable or static variable and they act just like a function (name-spaced) and they are deterministic base on only the input arguments , I want to ask, are ...
1
vote
4answers
453 views

Is this a candidate for Singleton?

I have an external thermometer connected via USB that is controlled by my SW. Different parts of my system will use it but never at the same time (all in one thread). However, it is a single device ...
25
votes
3answers
8k views

Why is a private member accessible in a static method?

The following is pseudo code, I tried it in Java and PHP and both worked: class Test { private int a = 5; public static function do_test(){ var t = new Test(); t.a = 1; ...