Title: An object reference is required for the non-static field
Description:
Each console application has it's own static main method. If we want to call a non static method in a static method it is not possible straight away because the static methods can be called without instantiate and non-static methods should need object instantiated before it get called. So, For this reason we have to create an object of class in main method to call the non-static methods. Now I will show you a simple example to call the non static method in static method.
Description:
Each console application has it's own static main method. If we want to call a non static method in a static method it is not possible straight away because the static methods can be called without instantiate and non-static methods should need object instantiated before it get called. So, For this reason we have to create an object of class in main method to call the non-static methods. Now I will show you a simple example to call the non static method in static method.
A small example:
//Program Class
class Program
{
public int add(int n1,int n2)
{
int result=n1+n2;
return result;
}
}
//Main method
static void main()
{
Program p=new Program();
p.add(n1,n2);
//logic
}
No comments:
Post a Comment