I was reading this statement:
"Difference is that instance methods have access to both the instance fields of the object instance and the static fields of the class, whereas static methods don’t have access to instance fields or methods."
I wanted to understand this with an example so I wrote this code:
Based on the statement I thought I couldn't have an instance of class A and an instance of the field InstanceField in the StaticMethod of class Program. There were no compile or run-time errors.
I'm pretty sure I haven't understood the statement correctly.
"Difference is that instance methods have access to both the instance fields of the object instance and the static fields of the class, whereas static methods don’t have access to instance fields or methods."
I wanted to understand this with an example so I wrote this code:
Code:
public class A
{
public int InstanceField = 4;
public static int StaticField = 5;
}
public class Program
{
static void StaticMethod()
{
A a = new A();
int InstanceField = a.InstanceField;
int StaticField = A.StaticField;
}
void InstanceMethod()
{
A a = new A();
int InstanceField = a.InstanceField;
int StaticField = A.StaticField;
}
}
I'm pretty sure I haven't understood the statement correctly.