The static-methods tag has no usage guidance.
3
votes
2answers
147 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)
...
2
votes
1answer
236 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 ...
3
votes
2answers
186 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.
...
5
votes
2answers
103 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 ...
0
votes
0answers
43 views
Should I split function reference into a class reference and method reference?
We have code like ['Index::Show::AjaxPost', ...] which references a function AjaxPost in Index::Show module.
I was ordained to refactor it in object oriented manner.
Should I split it into [['Index::...
4
votes
3answers
409 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
67 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 ...
9
votes
4answers
2k 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 ...
5
votes
1answer
553 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
196 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 ...
5
votes
4answers
2k 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
257 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 ...
2
votes
2answers
193 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
76 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
213 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
455 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
383 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
306 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 ...
4
votes
1answer
554 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
160 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
582 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
529 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
188 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
3k 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
435 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
990 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
421 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 ...
23
votes
3answers
6k 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;
...
0
votes
1answer
276 views
static or non-static, that is the question? [duplicate]
For comparisons / evaluations of objects of the same class(and other purposes), is it better to define a static or a non-static (is this called "dynamic" by chance?) function?
Sample code:
class ...
2
votes
1answer
9k views
Calling static method from instance of class
As a developer I am fan of static methods.
Recently I come across difference between OOP languages about this static methods.
All OOP language use static method to access any method without ...
13
votes
7answers
1k views
Is it a code smell if you are frequently creating an object just to call a method on it
I inherited a code base where there is a lot of code that goes something like this:
SomeDataAdapter sda = new SomeDataAdapter();
sda.UpdateData(DataTable updateData);
And then sda is never used ...
2
votes
4answers
10k views
Mocking static methods
Having recently returned from a Test Driven Development (TDD) course I had the following thought.
While writing unit tests using Mockito we came up against of the problem of mocking static methods. ...
1
vote
3answers
297 views
Refactoring - Utility classes behavior under a common interface
I was suggested to put my question here, so I'm doing so ;)
I need a common interface which represents some specific behavior:
public interface Contract(){
public void methodA();
public void ...
3
votes
3answers
796 views
Is it good to create static method at Visual Studio refactoring?
There are various refactoring options in Visual Studio to use at the time of coding. When we extract and create a new method. Then Visual Studio decide whether the new method will be static or ...
4
votes
5answers
629 views
Requring static class setter to be called before constructor, bad design?
I have a class, say Foo, and every instance of Foo will need and contain the same List object, myList.
Since every class instance will share the same List Object, I thought it would be good to make ...
3
votes
2answers
1k views
Should I put utility methods inside a class?
I have been working on a library which contains a large set of functions. For the sake of simplicity, I am going to use just one set as an example.
I am not sure which is the better way, in terms of ...
0
votes
2answers
359 views
Factory-Class versus Static Class
I've got a little problem in choosing the best design.
I have some (5 at the moment) image processing operations in my coe (Java). Every processing step independent from the other ones and consoists ...
0
votes
4answers
742 views
substitute for static inheritance
I currently have a number of classes (~20) that all do the same thing (abstractly), namely, generate an instance of a particular class from an xml file.
The way that they use the content of the xml ...
2
votes
2answers
446 views
Static or non-static?
For example I have these two classes:
First
public class Example
{
public Example()
{
}
public int ReturnSomething
{
return 1;
}
}
Second
public class Example
{
...
2
votes
1answer
2k views
JavaScript static methods retrieval
I have been thinking about it and wanted some feedback, recently I thought about doing it like this:
function foo(){
if ( !foo.prototype.statics ){
foo.prototype.statics = {
// ...
-1
votes
4answers
1k views
Immutable vs mutable object as returned parameter for class method [closed]
There is a class method (static method) in which I create and build some object. And for filling that object, I create it as mutable object.
My mutable object is a subclass of immutable object. So ...
3
votes
3answers
3k views
What is a static method compared to instance/class/private/public methods?
I'm learning programming in Objective-C and I can't understand what a static method is. I know what class/instance/private/public methods are. Can someone explain what it is using an example and ...
4
votes
1answer
509 views
How to implement syntax sugar in OO
The Problem
I regularly find myself writing the same code over and over again.
For example if I want a value from a nested array structure I end up writing something like this:
$config = ...
3
votes
3answers
3k views
OO PHP static keyword, should I use it?
I'm writing script for fb and I have 3 objects that I'll be using through all classes. I'm wondering if there is any advantage in using the static keyword except I don't have to create an instance ...
11
votes
3answers
302 views
“static” as a semantic clue about statelessness?
I've recently undertaken a refactoring of a medium sized project in Java to go back and add unit tests. When I realized what a pain it was to mock singletons and statics, I finally "got" what I've ...
2
votes
3answers
771 views
Syntactic sugar in PHP with static functions
The dilemma I'm facing is: should I use static classes for the components of an application just to get nicer looking API?
Example - the "normal" way:
// example component
class Cache{
abstract ...
4
votes
4answers
1k views
Could implicit static methods cause problems?
This is a purely hypothetical question.
Say I create a class method that contains no references to instance variables or other resources. For example (C#):
protected string FormatColumn(string ...
1
vote
3answers
302 views
Using static in PHP
I have a few functions in PHP that read data from functions in a class
readUsername(int userId){
$reader = getReader();
return $reader->getname(userId);
}
readUserAddress(){
$reader = getReader();...
52
votes
7answers
13k views
How to deal with static utility classes when designing for testability
We are trying to design our system to be testable and in most parts developed using TDD. Currently we are trying to solve the following problem:
In various places it is necessary for us to use static ...