3

Possible Duplicate:
Why can I only access static members from a static function?

While I was trying to call a normal method from inside a static method I got the error:

An object reference is required for the non-static field, method, or property

So this means I need to create the object of the Class and then call the nonstatic method. If I want to call the method directly then I have to declare that method as Static.

But , in this scenario , the calling method and called method belong to same class. So Why do I need to create an object while calling from a Static Method , while I can call a non-static method from non static method.

Ex:

class Program
    {
        //public void outTestMethod(int x,out int y)
        //{
        //    y = x;
        //}
        static void Main(string[] args)
        {
            int a = 10;
            int b = 100;

            outTestMethod(a,out b); 
        }

        private  void outTestMethod(int x, out int y)
        {
            y = x;
        }
    }

Error:An object reference is required for the non-static field, method, or property

Community
  • 1
  • 1
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • In your example you can just do `private static void outTestMethod` and it will work. You don't need to "create an object" as you said. – Icarus Oct 20 '11 at 14:15
  • The error means exactly what it says; a non-static (instance) method can access members of the Program instance (this particular example doesn't have any), whereas a static method can only access static members (since it has no instance to reference.) This is because it's possible to have multiple instances of a class. – Dan Bryant Oct 20 '11 at 14:16

5 Answers5

7

Static methods can call instance methods - but you need to have an instance on which to call them. It doesn't matter where that instance comes from particularly, so for example:

int a = 10;
int b = 100;
Program program = new Program();
program.outTestMethod(a,out b);

Instance methods are associated with a particular instance of the type, whereas static methods are associated with the overall type instead - and the same is true for other kinds of members. So to call an instance method, you need to know which instance you're interested in. For example, it would be meaningless to have:

string name = Person.Name;

because you need to know which person you're talking about:

Person person = FetchPersonFromSomewhere();
string name = person.Name;

... that makes much more sense.

Typically instance methods use or modify the state of the instance.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

Consider it this way.

A static method is the button outside a bank of elevators. Anyone can see it and push it, and make something happen (i.e. one of the elevators will arrive at that floor).

The non-static methods are the buttons inside a specific elevator. They manipulate THAT elevator (and none of the others).

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
1

A non-static method is also called an instance method. This means there (usually) is a chunk of data, specific to the instance (object) that the method operates on.

You can't call a non static or instance method from a static method because it wouldn't know which instance or object to operate on.

Jeffrey Sax
  • 10,253
  • 3
  • 29
  • 40
0

Because there is no instance to call the method from. YOu should create possibly another class and test on that:

class Program
{
    static void Main(string[] args)
    {
        int a = 10;
        int b = 100;
        Test testclass = new Test();
        testclass.outTestMethod(a,out b); 
    }
}

class Test
{

    public void outTestMethod(int x, out int y)
    {
        y = x;
    }
}
Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
0

do you understand the difference between instance method and static method?

an instance method has access to the this object even if it's not passed in as parameter, in fact it's like if it was an invisible parameter of the same type of the class passed for you by the framework.

a static method does not have that this object and cannot call instance methods because it does not have anything to pass for that this in the invisible way...

sounds like a joke but this is the way I see it :)

Davide Piras
  • 43,984
  • 10
  • 98
  • 147