Commit cbf1444
committed
feat: Add MethodAB with static max method to return larger of two integers
WHAT the code does:
Defines a MethodAB class with a static max(int x, int y) method.
Compares two integers and returns the larger value.
Demonstrates usage in main() by comparing two numbers (10 and 15) and printing the result.
WHY this matters:
Illustrates how to define and call static utility methods in Java.
Shows method return types and conditional branching with if-else.
Reinforces the difference between instance methods (requiring objects) and static methods (callable without an object).
Provides a simple pattern for reusable helper functions.
HOW it works:
main() declares two integers a = 10 and b = 15.
Calls max(a, b), which:
- Compares x and y
- Returns x if x is greater, otherwise returns y
Result (15) is printed to the console.
Tips and gotchas:
For concise code, max() could use Math.max(x, y), but implementing it manually helps build understanding.
Static methods cannot access instance variables directly; they belong to the class rather than an object.
If logic becomes more complex or depends on object state, refactoring into instance methods would be more appropriate.
Use-cases:
Basic demonstration of method creation, return values, and static usage in Java.
Reusable helper methods for mathematical operations.
Educational stepping stone before learning about method overloading and utility classes.
Short key: class-methodab static-method max-two-integers.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 75e6628 commit cbf1444
1 file changed
+3
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | | - | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
15 | | - | |
| 12 | + | |
16 | 13 | | |
17 | 14 | | |
0 commit comments