Commit add8a75
committed
feat: Add MethofAB1 to demonstrate static method invocation via object reference
WHAT the code does:
Defines a MethofAB1 class with a static max(int x, int y) method.
max() compares two integers and returns the larger one.
In main():
- Declares two integers a = 10 and b = 15
- Creates an object of MethofAB1
- Calls max() using the object reference, then prints the result (15)
WHY this matters:
Shows that static methods in Java can be invoked both through the class name and via an object reference.
Highlights that using an object reference for static calls is legal but discouraged, since it may confuse readers into thinking instance state is involved.
Demonstrates method return type usage and conditional logic with if-else.
HOW it works:
main() creates a MethofAB1 object named mp.
Calls mp.max(a, b), which executes the static method and returns the larger of a and b.
Since b = 15 is greater than a = 10, output is 15.
Tips and gotchas:
Static methods belong to the class, not any object instance; using an object reference to call them is misleading.
Best practice is to call static methods directly with the class name (MethofAB1.max(a, b)).
For clarity and maintainability, avoid mixing instance-style invocation with static members.
For comparison operations, Java provides Math.max(x, y) as a built-in utility.
Use-cases:
Educational demonstration of static method behavior and invocation styles.
Clarifies differences between static and instance members in Java.
Useful for teaching why static utility methods are usually accessed by class name.
Short key: class-methofab1 static-method object-invocation.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent ae03507 commit add8a75
1 file changed
+2
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
| 11 | + | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
| |||
0 commit comments