Why do we need static function overloading as shown below?
class Calculate
{ public static double res;
public static void calc(int x,int y)
{ res=x y;
}
public static void calc(float x,float y)
{ res=x*y;}
public static void calc(double x,double y)
{ res=x-y; }
public static void main(String[] args)
{ System.out.println("Calling and printing result of calc(5,3)( both integer variables)");
calc(5,3);
System.out.println("Result: " res);
System.out.println("Calling and printing result of calc(5.0,3.0)( both double variables)");
calc(5.0,3.0);
System.out.println("Result: " res);
System.out.println("Calling and printing result of calc(5.0f,3.0f)( both float variables)");
calc(5.0f,3.0f);
System.out.println("Result: " res);
}
}
if i remove the static from this code except for the one in main it gives the folowing errors
non static method calc(int,int)cannot be referenced from static context
calc(5,3);
non static variable res cannot be referenced from static context
System.out.print("Result= " res);
and similar errors for next function calls and print statements.
When do we need to declare methods and variables static??are there any set of rules and exception to it??