static-methods

In Java, is there any disadvantage to static methods on a class?

Lets assume that a rule (or rule of thumb, anyway), has been imposed in my coding environment that any method on a class that doesn't use, modify, or otherwise need any instance variables to do its work, be made static. Is there any inherent compile time, runtime, or any other disadvantage to doing this? (edited for further clarificati...

How to create static method that evaluates local static variable once?

I have a class with static method which has a local static variable. I want that variable to be computed/evaluated once (the 1st time I call the function) and for any subsequent invocation, it is not evaluated anymore. How to do that? Here's my class: template< typename T1 = int, unsigned N1 = 1, typename T2 = int, unsigned N2 =...

Static functions vs const functions

I'm looking at a member function int funct(int x) const; And I'm wondering if static int funct(int x); would be better. If a member function doesn't use any of the member variables should it be static. Are there any things that would discourage this? ...

Accessing non-static combbox property in the static method.

I have one combobox on the window form and I have one method which is declared with static like private static DataTable ParseTable(HtmlNode table) Now I want to use combobox in that method for using combobox property but I can not access any property of combobox or combobox itself.If I made the combobox declaration as static then it can...

Static property references non-static method

How can a static property reference a nonstatic method? Example: public static int UserID { get { return GetUserID(); } } private int GetUserID() { return 1; } When I try to compile this, I get the error: "An object reference is required for he non-static field, method or property "GetUserID()" ...

Java: when to use static methods

Hello, I am wondering when to use static methods? Say If i have a class with a few getters and setters, a method or two, and i want those methods only to be invokable on an instance object of the class. Does this mean i should use a static method? e.g Obj x = new Obj(); x.someMethod or Obj.someMethod (is this the static way?) I'...

When to make a method static?

I'd like to know how people decide whether to define a method as static. I'm aware that a method can only be defined as static if it doesn't require access to instance fields. So let's say we have a method that does not access instance fields, do you always define such a method as static, or only if you need to call it statically (withou...

mysql_close(): supplied argument is not a valid MySQL-Link resource

Here's what I'm trying to do: I've got a db.php file that does all the db manipulation. It has 2 static methods, connect and deconnect. In my other file i simply use db::connect() and db::deconnect(). The mysql_close($con) in the deconnect method just doesn't know who $con is. Since I don't want to instantiate my class static is the ...

Static variables in static method in base class and inheritance

I have these C++ classes: class Base { protected: static int method() { static int x = 0; return x++; } }; class A : public Base { }; class B : public Base { }; Will the x static variable be shared among A and B, or will each one of them have it's own independent x variable (which is what I want)? ...

helper functions as static functions or procedural functions?

i wonder if one should create a helper function in a class as a static function or just have it declared as a procedural function? i tend to think that a static helper function is the right way to go cause then i can see what kind of helper function it is eg. Database::connect(), File::create(). what is best practice? ...

iPhone: static method vs. sharedManager in Objective-C

I have a bunch of functions which returns commonly used UIViews in my application, e.g. + (UIView *) getLikeRow:(CGRect) frame ofType:(LikeType) type So far I've been using static methods for that, but recently I also noticed the sharedManager concept. Now I'm wondering if I should rather use a sharedManager for that. What are the di...

Static methods on ASP.NET web sites

Hi, i was wondering.. if i have a static method on an asp.net web site (plain vanilla), is that accessible by all users of all sessions? I guess what i am saying is the single instance of a method available to each client? or is there 1 instance for all clients for the site.. ...

Javascript - cannot make static reference to non-static function ....

I am making a reference to the Javascript function splice() on an array and I get the error: "Cannot make a static reference to the non-static function splice()" What's going on - how is this a static reference, aren't I referencing an instance of an Array class and its method - how is that static? $(document).ready( function() { v...

When calling a static method on parent class, can the parent class deduce the type on the child (C#)?

Suppose we have 2 classes, Child, and the class from which it inherits, Parent. class Parent { public static void MyFunction(){} } class Child : Parent { } Is it possible to determine in the parent class how the method was called? Because we can call it two ways: Parent.MyFunction(); Child.MyFunction(); My current approach wa...

Create Generic Class Instance from Static Method in a Derived Class

I have a class in C# with a template and static method similar to class BClass<T> { public static BClass<T> Create() { return new BClass<T>(); } } From this I derive a class and specify a template parameter to the base class class DClass : BClass<int> { } A problem occurs when I try to use the static method to create an ...

"Abstract static" method - how?

There are already several SO questions on why there is not abstract static method/field as such, but I'm wondering about how one would go about implementing the following psuedo-code: class Animal { abstract static int getNumberOfLegs(); // not possible } class Chicken inherits Animal { static int getNumberOfLegs() { return 2; ...

Create an instance from a static method

Hello, let's say I want my users to use only one class, say SpecialData. Now, this data class would have many methods, and depending on the type of data, the methods do different things, internally, but return externally similar results. Therefore my wanting to have one "public" class and other "private", child classes that would chang...

To static or not to static

I really like to use static methods (especially for helpers classes). But as static methods are not stubbable, eventually they are a bad practice, aren't they? So I have to choose between static methods usage convenience and testability. Is there any compromise? ...

Ruby: Alter class static method in a code block

Given the Thread class with it current method. Now inside a test, I want to do this: def test_alter_current_thread Thread.current = a_stubbed_method # do something that involve the work of Thread.current Thread.current = default_thread_current end Basically, I want to alter the method of a class inside a test method and recover ...

Why can't you call a non-static method from a static method?

I have a static method [Method1] in my class, which calls another method [Method2] in the same class and is not a static method. But this is a no-no. I get this error: An object reference is required for the non-static field, method, or property "ClassName.MethodName()" Can someone please briefly describe why? including other t...