P2_5.
What is Method Overloading?
In Java, you can use the same method name to describe different methods. = Doing so is called method overloading. We=92re allowed to overload because= Java distinguishes methods not just by their names, but by the numbers,= types, and orders of their parameter lists. In other words, two methods= with the same name can be quite different from Java=92s point of view= because one has may have double inputs, while the other, integer inputs,= etc.
As an example of overloading, consider these two methods:
Boolean isEqualTo(int b)
{
return (a=3D=3Db);
}
boolean isEqualTo(double y, double TOL)
{
return (Math.abs(x-y) < TOL);
}
Java can distinguish between these methods because one takes 2= parameters, the other 3. And even if the second version were to have two= parameters (say TOL was declared inside the method), Java would be able to= tell which one you were calling by inspecting whether the inputs you= actually entered when you called these methods were of type integer or of= type double. For instance a call to obj.isEqualTo(5.0) would indicate to= Java that you wanted the method with double inputs.
Static variables and static methods
Every object of a class has its own copy of the= instance variables defined for that class. But maybe you might want only= one copy of a particular variable to be shared by all objects of the= class.
<example>
Say you wanted to keep track of the number of objects that have been= instantiated from a particular class, then the variable which contains that= number has to be such that it has the same value for all the objects. All= the objects need to know how many objects there are presently.
</example>
Static class members exist even when no objects of that class exist. To= access a public static class member when no objects of that class are= present, we can simply prefix the class name and the dot operator to the= classmember.
<example>
If class MyClass has a public static variable x, we can refer to x using= MyClass.x without having to create a new object of type MyClass using the= new operator.
</example>
A method declared static cannot access nonstatic class members. It is,= in fact, a syntax error for a static method to call on an instance method= or to access an instance variable=97as some of you have no doubt discovered= while debugging your programs.