1

As we know, In java-8 we can store function/method in variable. by using following way.

    @FunctionalInterface
    public interface MyInterface {

      public string getValue(int val1, int val2);

    }


    public class MyClass {

    static String someFun(int val1, int val2) {
       return ""+(val1+val2)

    }

     static BiFunction<Integer, Integer, String> testParamFun = (a,b) -> ""+(a+b);

    public static void main(String[] args){

       MyInterface intr = MyClass::someFun;
       System.out.println(int.getValue(2,4)); // outpur will be "6" 

       /* i want this, but it give me compile time error? 
         I want to store that function in variable like i was doing in above case. */

   MyInterface inter = MyClass::testParamFun;
   System.out.println(inter.getValue(4,5)); // it gives me error.
   // then i tried this
   System.out.println(inter.apply(4,5)); // i got error again. 


    }

    }

My question is, how can I store BiDirection in variable type MyInterface

8
  • I'm not sure I understand your end goal here. Could you clarify a bit? Commented Sep 11, 2015 at 10:41
  • If you're going to post demonstration code, could it be working demonstration code? Commented Sep 11, 2015 at 10:43
  • i edited my question. Commented Sep 11, 2015 at 10:53
  • 2
    You cannot name your variable as int. Commented Sep 11, 2015 at 10:53
  • 2
    Your code contains lots of spell mistakes. Maybe your problem disappears once you fix all these errors first? Commented Sep 11, 2015 at 11:51

1 Answer 1

5

You need to refer to the BiFunction's method:

MyInterface int_ = MyClass.testParamFun::apply;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.