Skip to content

Commit 762312f

Browse files
committed
feat: demonstrate method references (static, instance, and predefined) in Java
WHAT was done: - Created a functional interface `MyRefrences` with a single abstract method `display(String str)`. - Implemented three variations of method references: 1. **Static method reference** → `MethodRefrences::reverse` - Points to a static method that reverses a string using `StringBuffer.reverse()`. 2. **Instance method reference** → `MD::reverse1` - Points to a non-static method of a specific object. 3. **Predefined method reference** → `System.out::println` - Refers to Java’s built-in `println` method to directly print strings. KEY LEARNINGS: 1. Method Reference Basics: - Syntax: `ClassName::methodName` (static) or `object::methodName` (instance). - They provide a shorthand for writing simple lambda expressions. - Example: `str -> System.out.println(str)` ≡ `System.out::println`. 2. Types of Method References: - Static method reference → `ClassName::staticMethod`. - Instance method reference (specific object) → `object::instanceMethod`. - Instance method reference (arbitrary object) → `ClassName::instanceMethod`. - Constructor reference → `ClassName::new`. 3. Advantage: - Improves readability by eliminating boilerplate. - Useful when lambda just calls an existing method. EXAMPLE EQUIVALENCE: - Lambda: `(s) -> System.out.println(s)` - Method reference: `System.out::println` REAL-LIFE APPLICATIONS: - ✅ Printing collections with `forEach(System.out::println)`. - ✅ Sorting with comparators (`String::compareToIgnoreCase`). - ✅ Stream API transformations where existing methods suffice. - ✅ Reusing existing utility methods (e.g., `Integer::parseInt` in a mapping function). RULE OF THUMB: - If your lambda only calls an existing method, prefer method references. - Keeps code concise and more expressive. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 10a608c commit 762312f

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
interface MyRefrences
2-
{
1+
interface MyRefrences {
32
public void display(String str);
43
}
54

6-
public class MethodRefrences
7-
{
8-
public static void reverse (String str)
9-
{
10-
StringBuffer sb=new StringBuffer(str);
11-
sb.reverse(); //Reverse method inside string buffer
5+
public class MethodRefrences {
6+
public static void reverse(String str) {
7+
StringBuffer sb = new StringBuffer(str);
8+
sb.reverse(); //Reverse method inside string buffer.
129
System.err.println(sb);
1310
}
1411

1512
//If the method is not static.
16-
public void reverse1(String str)
17-
{
18-
StringBuffer sb=new StringBuffer(str);
19-
sb.reverse(); //Reverse method inside string buffer
13+
public void reverse1(String str) {
14+
StringBuffer sb = new StringBuffer(str);
15+
sb.reverse(); //Reverse method inside string buffer.
2016
System.err.println(sb);
2117
}
2218

23-
public static void main(String[] args)
24-
{
19+
public static void main(String[] args) {
2520
MethodRefrences MD = new MethodRefrences();
26-
MyRefrences R3=MD::reverse1;
21+
22+
MyRefrences R3 = MD::reverse1;
2723

2824
MyRefrences R2=MethodRefrences::reverse;
2925
//This is a like Scope resolution in C++.
@@ -32,9 +28,7 @@ public static void main(String[] args)
3228
//System = Class and Out = is an object inside that class and println = is static.
3329

3430
R1.display("Hello");
35-
3631
R2.display("Hello");
37-
3832
R3.display("Non Static Method");
3933
}
40-
}
34+
}

0 commit comments

Comments
 (0)