Skip to content

Commit cbf1444

Browse files
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

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

Section10Methods/src/MethodAB.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
public class MethodAB
2-
{
1+
public class MethodAB {
32
static int max(int x, int y) //Return type int.
43
{
54
if(x>y)
65
return x;
76
else
87
return y;
98
}
10-
public static void main(String[] args)
11-
{
9+
public static void main(String[] args) {
1210
int a =10;
1311
int b= 15;
14-
System.out.println(max(a,b)); //Main method is static. and static method called only static methpds.
15-
12+
System.out.println(max(a,b)); //Main method is static. and static method called only static methods.
1613
}
1714
}

0 commit comments

Comments
 (0)