![]() ![]() In main method we have Optional variable of String. Person class need a parameter to create new object. Similarly as previous example we have person class. ![]() If we are just creating new object in lambda expression, we can use constructor reference.ĬlassName :: new Example of constructor method reference in java In the example we have called printDetails method on the person object. Person.ifPresent(obj -> obj.printDetials()) Optional person = Optional.of(new Person("Mac")) We have to call printDetails method for the object. We have created an Optional variable of person type. Person class is having method printDetails. In this case the class type of object is used to called method.ĬlassName :: methodName Example of method reference by class type in java Then we can use Method reference using class type. In the other words we can say the lambda expression scenario where we are just calling method and object is a input as from argument. Method reference can be used, if we have method calling from object while object is provided by context. Object method reference By class type in java In the example we have called print method using lambda expression and equivalent Object method reference format. MethodReferenceTest obj =new MethodReferenceTest() ![]() First of all we have removed static from print method so it is a normal method now. We will use same example as static method reference, with the small change. However if in lambda expression we just have method call using object, We can refer such methods using class objects.ĬlassObject :: methodName Example of object method reference in java In java if we need objects to call methods. In the example we have called print method using lambda expression and equivalent Static method reference format. Msg.ifPresent( MethodReferenceTest::print) Using Method reference to print method from MethodReferenceTest class Msg.ifPresent( text->MethodReferenceTest.print(text)) Optional msg = Optional.of("StackTraceGuru") Then call print method for the variable using class name. ![]() In main method we have created an Optional variable. In the example we have MethodReferenceTest class and static method print. StaticClass :: staticMethod Example of java static method reference Similarly, we can refer static methods using class name. Different ways of method references in java Reference TypeĪs we know in java we can call static methods using class name without creating object. The most important point to note is we can only use method reference for lambda expression having single line of another method call. Arguments are provided by the context variables. Most noteworthy point to notice is that in method reference syntax we use " :: " operator not the ". These are the cases when we can use java 8 method reference so that code will be clean and easy to read. Users.stream().forEach( user-> userService.update(user) ) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |