static-methods

Static variables and methods

I ran across a class that was set up like this: public class MyClass { private static boolean started = false; private MyClass(){ } public static void doSomething(){ if(started){ return; } started = true; //code below that is only supposed to run //run if not started } } My understanding with sta...

Using private static methods

What do you think about using private static methods? Personally, I prefer using a static private method to non-static as long as it does not require access to any instance fields. But I heard that this practice violates OOP principles. Edit: I am wondering from style prospective of view, not performance. ...

Static methods on generic classes?

Hi all.. Okay, this is the case: I got a generic base-class which I need to initialize with some static values. These values have nothing to do with the kind of types my generic baseclass is loaded with. I want to be able to do something like this: GenericBaseclass.Initialize(AssociatedObject); while also having a class doing like ...

Is using a lot of static methods a bad thing?

I tend to declare as static all the methods in a class when that class doesn't require to keep track of internal states. For example, if I need to transform A into B and don't rely on some internal state C that may vary, I create a static transform. If there is an internal state C that I want to be able to adjust, then I add a constructo...

C#: ReSharper complains when method can be static, but isn't

Why does ReSharper complain when a method can become static, but is not? Is it because only one instance of a static method is created (on the type) and thus save on performance? ...

Can I get the same benefits of functional programming (F#) by using more static methods in C#?

I admit I haven't grokked F# yet. But in the 30,000 foot descriptions, they keep talking about easy to test code that doesn't have mutable state. Is that the same as static methods? Could I get the benefit of F# by writing some of my code in classes with all static methods? I'm just looking for the short answer, I know whole books exi...

Static methods vs repository pattern with Linq2Sql

I've hit on the idea of creating static methods on the partial Linq queries such as public partial class User { public static User FindByGuid(string guid, ApplicationDataContext context) { return context.Users.Where(x => x.GUID == guid).Single(); } } So, for example, I can easily find a user by doing: using (var c...

Help reviewing the following code, is it thread safe?

private static Callback callback; public Foo() { super(getCallback()); } private static Callback getCallback() { callback = new Callback(); return callback; } Constructor Foo() can potentially be called from multiple threads. My concern is with the private static field 'callback' and the static method 'getCallback()'. A...

Static method in a generic class?

In Java, I'd like to have something as: class Clazz<T> { static void doIt(T object) { // shake that booty } } But I get Cannot make a static reference to the non-static type T I don't understand generics beyond the basic uses and thus can't make much sense of that. It doesn't help that I wasn't able to find much info on the ...

Calling static method on a class?

Say, I have a reference to a Class object with SomeType having a static method. Is there a way to call that method w/o instantiating SomeType first? Preferably not escaping strong typing. EDIT: OK, I've screwed up. interface Int{ void someMethod(); } class ImplOne implements Int{ public void someMethod() { // do something } } Cl...

How do I infer the class to which a @staticmethod belongs?

I am trying to implement infer_class function that, given a method, figures out the class to which the method belongs. So far I have something like this: import inspect def infer_class(f): if inspect.ismethod(f): return f.im_self if f.im_class == type else f.im_class # elif ... what about staticmethod-s? else: ...

F# Multiline Static Methods

I'm sure this should be obvious, but how do i have a multiline static method in a class? i just can't figure it out... ...

Are static methods in Java always resolved at compile time?

Are static methods in Java always resolved at compile time? ...

Objective-C: Class vs Instance Methods?

Hi, What's the difference between a class method and an instance method? Are instance methods the accessors (getters & setters) while class methods are pretty much everything else? Thanks, ...

Static method in Java

Looking through some java code and this just does not seem right. To me, it looks like every time you call projects, you will get a new hashmap, so that this statement is always false projects.get(soapFileName) != null Seems like it should have a backing field public static HashMap<String,WsdlProject> projects = new HashMap<String,Ws...

Return class name in which a static method resides

Hi Consider the following code: public class MyClass { public static string MyStaticMethod() { //string className = GetClassNameHere... } } Is it possible to get the name of the class in which the static method resides ? Due to the fact that im using a static method, it is not possible to use the this pointer...

Java : What is - public static<T> foo() {...} ?

I saw a java function that looked something like this- public static<T> foo() {...} I know what generics are but can someone explain the in this context? Who decides what T is equal to? Whats going on here? EDIT: Can someone please show me an example of a function like this. ...

Fluent Interfaces - the number of objects being created

Hi guys I am in the process of creating some fluent interfaces for some simple validation stuff that I am playing around with. One thing that I have noticed is that I have a lot of different objects being created. For instance given the below statements: Check.Assertion.ForValue.That(value, "value").IsNotNull() : void Check.Assertio...

C#: Can an _object_ override a class' method?

Hi, I have a sample class: class SampleClass { public virtual string SomeProperty{get; set;} public virtual void SomeMethod() { // code } } I can Inherit from it and override SomeProperty and SomeMethod like this: class ChildClass:SampleClass { public override string SomeProperty{get; set;} public override void S...

Should I use static methods for DataAccess common operators?

I'm not using hibernate and that's not a valid alternative at all. I developed a framework based on ADO.NET. This framework has default operators for each database entity. For example, data access layers has default operators as the following: Insert Update Delete Get GetAll To access this methods, we work as tha sample code below: ...