I have a class Program. Here it has main method.It is static method. I created a simple getAllSum method to return sum of three values.But, if i remove static keyword then i get error as:
**Cannot make a static reference to the non-static method getALlsum(int, int, int) from the type Program
**
If I am calling a function from static method to non static method then,why it is necessary to become non static method to be static?
Static method I have learned about them is that:
- Can be called from class name instead of using object.
- Each object shares the same variable.
But,I am getting confuse why cant we call a nonstatic function from static function?What is the reason behind this?
public class Program {
public static void main(String[] args) {
int l=getALlsum(1,2,3);
System.out.println(l);
}
public static int getAllSum(int a,int b,int c) {
return (a+b+c);
}
}