Prasanna Peethambaram’s Post

: : Method References 👉 It’s all about who is the object. 🧠 1️⃣ Static Method Reference ClassName::staticMethod No object needed. Just direct method mapping. Equivalent to: x -> ClassName.method(x) 🧠 2️⃣ Instance Method Reference (Specific Object) object::method Here, the object is fixed. The stream elements are passed as arguments. Equivalent to: x -> object.method(x) The object never changes. 🧠 3️⃣ Arbitrary Object Method Reference ClassName::instanceMethod The class name is just type information. The stream element is the real object. This is where it gets interesting. Here, each stream element becomes the object. Equivalent to: x -> x.method() The object changes for every element. The method stays the same. That’s the real difference. 👍 Instance method reference (specific object): one fixed object, stream elements are just arguments to its method. 👍 👍 Arbitrary method reference: each stream element becomes the object, and its own method is called. 🧠 4️⃣ Constructor Reference ClassName::new Just a cleaner version of: x -> new ClassName(x)| 💡 The Big Realization Method references are not magic. They are just cleaner lambdas. And they only work because of functional interfaces GitHub Link: https://lnkd.in/gUr-ifF8 🔖Frontlines EduTech (FLM) #java #MethodReferences #Java8 #FunctionalProgramming #LambdaExpressions #StreamsAPI #OOPDesign #BackendDevelopment #Programming #CleanCode #ResourceManagement #AustraliaJobs #SwitzerlandJobs #NewZealandJobs #USJobs #MethodReferenceOperator

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories