0

I'm currently started to learn Lambda Expressions in Java and I'm trying to figure out why this code doesn't work I have a general class:

import java.util.function.ToIntBiFunction;


public class testclass {

    public testclass() {

    }
    public static ToIntBiFunction<Integer,Integer> multit2 = (Integer a,Integer b)->{
        return a*b;

    };

    public static Integer multit(Integer a, Integer b) {
        return a*b;
    }
}

and the main in another class

public class ImageCovertor{

public static void main (String[] args) throws IOException {

    int d;
    testclass test = new testclass();

    BiFunction<ToIntBiFunction,Integer, Integer> ddd = (ToIntBiFunction fn, Integer a)->{
        return fn.applyAsInt(a,a);
    };
    d= ddd.apply(testclass::multit, 5);
    System.out.println(d);

}//end of main
public static ToIntBiFunction<Integer,Integer> multit = (Integer a,Integer b)->{
    return a*b;

};


}

and I'm trying to pass the multit as a parameter in the ddd lambda fuction but this like of code gives me an error:

d= ddd.apply(testclass::multit, 5);

The type testclass does not define multit(Object, Object) that is applicable here

also tried to make the function in the main but it gives me the same error the code works when I write

testclass.multit

instead of

testclass::multit

can someone explain to me why the second one doesn't work and how to fix this? thank you

eyal mazuz
  • 109
  • 1
  • 2
  • 10
  • There is no method `multit(...)` within `testclass`. You have a static field `multit` within `ImageConvertor`. – Turing85 May 11 '18 at 19:57
  • @Turing85 it was my mistake I copied the wrong name, now I fixed it – eyal mazuz May 11 '18 at 20:01
  • You are using a [raw type](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) in `ddd`'s type definition (`ToIntBiFunction` shoudl be `ToIntBiFunction`). Also, to avoid unnecessary casting, method `multit` in `testclass` should return an `int` instead of an `Integer`. – Turing85 May 11 '18 at 20:09
  • @Turing85 thank you changing to 'BiFunction' worked fine, I guess I should have let my function know the type of function it gets and what type of paramaters. thank you – eyal mazuz May 11 '18 at 20:15

1 Answers1

1

testclass::multit means a method multit belonging to testclass. What you have is a field testclass.multit that holds a function. A field holding a function is not the same as a method.

class MyClass {
    // This is a method, `MyClass::foo`
    public static Integer foo(Integer a, Integer b) {
        return a*b;
    }
    // This is a field holding a function, `MyClass.bar`
    public static ToIntBiFunction<Integer, Integer> bar = (Integer a,Integer b)-> {
        return a*b;
    };
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • tried changing public static int multit(int a, int b) { return a*b; } to testclass and then useing testclass::multit it gives me the same error – eyal mazuz May 11 '18 at 20:03
  • @eyalmazuz I can't solve new problems in code I haven't seen. – khelwood May 11 '18 at 20:04
  • @eyalmazuz of course it does. Again `testclass::multit` means that class `testclass` has a **method** `multit`. But what you have is a **field** of type `ToIntBiFunction`. – Turing85 May 11 '18 at 20:05