Reputation: 20239
Suppose you have some method that could be made static, inside a non-static class.
For example:
private double power(double a, double b)
{
return (Math.Pow(a, b));
}
Do you see any benefit from changing the method signature into static? In the example above:
private static double power(double a, double b)
{
return (Math.Pow(a, b));
}
Even if there is some performance or memory gain, wouldn't the compiler do it as a simple optimization in compile time?
Upvotes: 32
Views: 24242
Reputation: 50041
Do you see any benefit from changing the method signature into static?
Three benefits:
Making stateless methods static helps document and clarify their purpose. Otherwise, one is inclined to worry just what mysterious state does the method depend upon?
The static method can be called from other static code, so is potentially more useful.
Static method calls have a smidgin less runtime overhead than instance ones. The compiler can't do that transform automatically -- one reason why is because it would affect use of null. Calling the method on a null reference is required to fail with a NullReferenceException, even if there is no instance state used within the method.
Upvotes: 3
Reputation: 21
Members that do not access instance data or call instance methods can be marked as static (Shared in Visual Basic). After you mark the methods as static, the compiler will emit nonvirtual call sites to these members. Emitting nonvirtual call sites will prevent a check at runtime for each call that makes sure that the current object pointer is non-null. This can achieve a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.
Upvotes: 2
Reputation: 1265
i hope we need to define the difference between static and non-static method. Here i am posting few easy lines of code to visualize the conceptual difference.
public class StaticVsNonStatic
{
public string NonStaticMethod() //non-static
{
return "I am the Non-Static Method";
}
static public string StaticMethod() //static
{
return "I am Static Method";
}
}
Now lets create an aspx page try to access these 2 methods defined in the class.
public partial class StaticVsNonStatic_StaticVsNonWorkplace : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StaticVsNonStatic objStaticVsNonStatic = new StaticVsNonStatic();
lblDisplayNS.Text = objStaticVsNonStatic.NonStaticMethod(); //Non Static
lblDisplayS.Text = StaticVsNonStatic.StaticMethod(); //Static and called without object
}
}
Thanks and please post you comments.
Best Regards, Pritom Nandy [Bangladesh]
Upvotes: -4
Reputation: 20151
Note that it is highly unlikely the compiler is even allowed to make that change on your behalf since it changes the signature of the method. As a result, some carefully crafted reflection (if you were using any) could stop working, and the compiler really cannot tell if this is the case.
Upvotes: 9
Reputation: 241779
As defined, power
is stateless and has no side effects on any enclosing class so it should be declared static
.
This article from MSDN goes into some of the performance differences of non-static versus static. The call is about four times faster than instantiating and calling, but it really only matters in a tight loop that is a performance bottleneck.
Upvotes: 36
Reputation: 7209
this method should be static because it is not related to your class or member of classes. it just works with the inputs to this function.
maybe you may need to call it without creating that class. so if it is static it is ok but if it is not, you cant call it without any instance of that class.
Advantages of a static method:
There's no need to create an object first. The method is available immediately.
It's a good when you have generic functionality that does not depend on the state of a particular object. For example, look at the Arrays class or the Collections class from java.util.
Static methods can be good for factories. Pass your requirements as parameters, get a brand new object back. Not a constructor in sight.
Disadvantages of a static method:
You don't have an object instance, so you only have access to static members and your own local variables. If you want an instance, you probably have to create it yourself.
You can create subclasses, but a static method can't be abstract, so it's harder to decouple your implementation from a caller.
(ps: i dont think compiler will optimize if it is going to be static or not.. but not sure)
Upvotes: 1
Reputation: 30055
The compiler will likely consider inlining this when it "JITs" the code as it's so short and if it does so then will likely be able to optimise out any reference to the unused this parameter. But you can't rely on any of that.
You still have to make an object to call it on unless you make it static which has a much bigger overhead anyway if you don't need one for other reasons.
Upvotes: 1
Reputation: 29956
I believe the compiler will not optimise this into a static method. There is a performance gain since the pointer will not have to be checked to see if it is null at runtime. Have a look at the FXCop rule for reference.
Upvotes: 0
Reputation: 292685
There should be a slight performance improvement if you declare the method static, because the compiler will emit a call
IL instruction instead of callvirt
.
But like the others said, it should be static anyway, since it is not related to a specific instance
Upvotes: 8
Reputation: 4807
To me an easy question to decide is "Should I have to instantiate this object just to call this function". In the case of your function, I would say the answer is no, so it should be static. This method does not seem to be related to your object so again, I vote static.
Upvotes: 2