One non-static class is having static and non-static method. Then how to call non-static method in static method? And vice versa.

If you call a non-static method in static method then you should make an object of that non-static method and call it. Example- 

public class myNonStaticClass
    {
        public static string myStaticMethod()
        {
            //to call non-static method from static method --- as like any other class nonstatic method
            myNonStaticClass obj = new myNonStaticClass();
            return obj.myNonStaticMethod();
        }
        public string myNonStaticMethod()
        {
            return "hi from non static method";
        }
    }

If you call a static method in non-static method you can directly call that method/function. Example-

  public class myNonStaticClass
    {
        public static string myStaticMethod()
        {
            return "hi from static method";
        }
        public string myNonStaticMethod()
        {
            return myStaticMethod();
        }
    }


Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*