Skip to content

Commit add8a75

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

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

Section10Methods/src/MethofAB1.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
public class MethofAB1
2-
{
1+
public class MethofAB1 {
32
static int max(int x, int y) //Return type int.
43
{
54
if(x>y)
@@ -9,7 +8,7 @@ static int max(int x, int y) //Return type int.
98
}
109
public static void main(String[] args) {
1110
int a=10,b=15;
12-
//Object of Class
11+
//Object of Class.
1312
MethofAB1 mp = new MethofAB1();
1413
System.out.println(mp.max(a,b)); //Just like obj of string we do it in previous
1514
}

0 commit comments

Comments
 (0)