2

I have the following code line:

MyClass{
    Runnable job; 
    ...    
}

and inside one of the method:

this.job = myImporter::importProducts;

Now importProducts is method without arguments:

public void importProducts() {
        ...
}

But I need to add argument to this method now.

After adding new argument, line:

this.job = myImporter::importProducts;

became broken.

Is it possible to fix it?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 3
    What type is `this.job`? And how does `importProducts` look after the change? – Eran Jun 08 '15 at 08:41
  • `became broken?` You have a compiler error? How about sharing it? – Will Jun 08 '15 at 08:42
  • Your question is very unclear. Can you provide example with actuall code which we could use to reproduce your problem? – Pshemo Jun 08 '15 at 08:55

1 Answers1

2

It's not possible to "bind" and argument directly to the method reference. In this case you can easily use lambda:

this.job = () -> myImporter.importProducts(myNewArgument);

Alternatively if it fits your situation consider leaving the zero-arguments importProducts method which just calls the one-argument importProducts with proper argument value:

public void importProducts() {
    importProducts(myNewArgument);
}

private void importProducts(Type arg) {
    ...
}

This way your method reference will work as before.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334