Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods in a class - okay in this situation?

Hey all - I have an app where I'm authenticating the user. They pass username and password. I pass the username and password to a class that has a static method. For example it'm calling a method with the signature below:

public class Security
{
public static bool Security.Member_Authenticate (string username, string password) 
{ //do stuff}
}

If I have 1000 people hitting this at once, will I have any problems with the returns of the method bleeding into others? I mean, since the methods are static, will there be issues with the a person getting authenticated when in fact they shouldn't be but the person before them was successfully authenticated ASP.Net returns a mismatched result due to the method being static? I've read of issues with static properties vs viewstate but am a bit confused on static methods. If this is a bad way of doing this,what's the prefered way?

like image 980
Bill Martin Avatar asked Jan 21 '09 18:01

Bill Martin


People also ask

Are static methods OK?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous. Overriding a static method is not that simple for some languages.

When should static methods be used in class?

Static methods are usually preferred when: All instance methods should share a specific piece of code (although you could still have an instance method for that). You want to call method without having to create an instance of that class. You must make sure that the utility class is never changed.

Is it OK to use static methods Java?

The static method can modify static members of the class (which may be private). This can be useful. For example, you could have a method like "static synchronized int allocateID() {return idNext++;}". In fact, a static method can be just as pure or impure as a non-static method in terms of side effects.

Can static methods be used in other classes?

Calling static methodsIf a method (static or instance) is called from another class, something must be given before the method name to specify the class where the method is defined. For instance methods, this is the object that the method will access. For static methods, the class name should be specified.


1 Answers

This will not happen. When a method is Static (or Shared in VB.NET), then you're safe as long as the method doesn't rely on anything other than the inputs to figure something out. As long as you're not modifying any public variables or objects from anywhere else, you're fine.

like image 69
SqlRyan Avatar answered Sep 21 '22 23:09

SqlRyan