Java lets several methods share a name if they differ in their parameters. It also lets some members belong to the class itself rather than to each object.
Method Overloading
Method overloading is defining two or more methods in the same class with the same name but different parameter lists: a different number of parameters, different types, or a different order of types. The compiler chooses the method to call by matching the arguments, which is compile-time (static) polymorphism. The return type alone cannot distinguish overloaded methods. For example, add(int, int), add(double, double) and add(int, int, int) can all exist together. The println method is itself overloaded for every type.
Definition: Method overloading. Defining several methods in one class with the same name but different parameter lists, the correct one being chosen by the compiler from the arguments of the call.
Constructor Overloading
Constructors can also be overloaded, giving several ways to create an object: Box(), Box(double side) and Box(double l, double w, double h). One constructor may call another with this(...), which must be its first statement.
Static Members
A static field belongs to the class, not to any object, and there is only one copy, shared by all objects. It is used for counters and constants: static int count;. A static method also belongs to the class and is called with the class name, such as Math.sqrt(). A static method can use only static fields and other static methods directly, and cannot use this, since there may be no object. main is static so that the JVM can call it without an object. A static block runs once when the class is loaded, to initialise static fields.
Nesting of Methods
A method can call another method of the same class directly by name, without the dot operator. This is called nesting of methods.
Table 3.2: Instance and static members
| Feature | Instance member | Static member |
|---|---|---|
| Belongs to | Each object | The class |
| Copies | One per object | One in total |
| Access | object.member | ClassName.member |
| Can use this | Yes | No |
Example 3.2: Overloading and a static counter
Problem: Write a class Calc with overloaded add methods for two ints, two doubles and three ints, and a static count of the calls made.
Solution:
- Write add(int, int), add(double, double) and add(int, int, int).
- Each increments the static field count.
- main calls each once and prints count through the class name.
Result: The three sums and a count of 3 are printed.
public class Overload {
public static void main(String[] args) {
System.out.println(Calc.add(2, 3));
System.out.println(Calc.add(2.5, 3.5));
System.out.println(Calc.add(1, 2, 3));
System.out.println("calls = " + Calc.count);
}
}
class Calc {
static int count = 0;
static int add(int a, int b) { count++; return a + b; }
static double add(double a, double b) { count++; return a + b; }
static int add(int a, int b, int c) { count++; return a + b + c; }
}
Output
5
6.0
6
calls = 3
Common mistakes
- Trying to overload methods that differ only in return type.
- Using an instance variable inside a static method.
- Thinking each object has its own copy of a static field.
Key takeaways
- Overloaded methods share a name but differ in parameters.
- Overloading is resolved at compile time.
- Static members belong to the class and have one copy.
- Static methods cannot use this or instance fields directly.
Practice questions
- What is method overloading?
- Can two methods differ only by return type?
- Why is main static?
Answers to the practice questions
- Defining several methods with the same name but different parameter lists in one class.
- No; the parameter lists must differ.
- So that the JVM can call it without creating an object.
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit III
