Static vs. non-static method

前端 未结 10 1037
执念已碎
执念已碎 2020-11-28 08:30

Suppose you have some method that could be made static, inside a non-static class.
For example:

private double power(double a, double b)
    {
        r         


        
相关标签:
10条回答
  • 2020-11-28 09:25

    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]

    0 讨论(0)
  • 2020-11-28 09:29

    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.

    0 讨论(0)
  • 2020-11-28 09:29

    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.

    0 讨论(0)
  • 2020-11-28 09:32

    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)

    0 讨论(0)
提交回复
热议问题