• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Void Compatible lambda expressions

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

For the below code, I was expecting the output as "nothing return testM1". But  the method "void myM1(String s2)" is not getting called. The
interface A, and statement expression and the implementation method is having the same void return type with one string as a parameter. But I did not understand, why it is not called ?  Can you please help me understand.

import java.util.ArrayList;


interface A { public void voidAbstractMethod(String s2);}

class Demo {
  public static void main(String[] args) {
       A s1 = s -> new ArrayList().add(s);
      s1.voidAbstractMethod("testM1");
  }

  void myM1(String s2) {
      System.out.println("nothing return "+s2);
 }
}

 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear to have forgottne how to use code tags, and also that you shouldn't mix tabs and spaces for indenting (use spaces only please).
Anyway, there is a simple reason why you don't get method myM1() called. You haven't called it anywhere. Your voidAbstractMethod() (now, isn't that even worse as a method name!) has no effect on program state because the List is created as a variable loacl to the λ, and goes out of scope as soon as it is used.
 
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srini saitala wrote:But I did not understand, why it is not called ?  Can you please help me understand.


Please help us understand why you think the myM1() method should be executed by that code? What is the sequence of events that you think is going to occur such that it would be called?
 
Saloon Keeper
Posts: 1327
40
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint, the @Override annotation never does anything useful for currently working code.

But boy does it do wonders for code that contains any errors regarding implementation of abstract methods in interfaces or either abstract or concrete methods in subclasses!

Did you intend for your class Demo to implement interface A?  It does not.

If it did, when it does, you would still need an object of your class Demo to call your method, it is an instance method, not a static method.
 
Campbell Ritchie
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer to use @Override as I am writing the code. Then the annotation can produce an error at compile time. It isn't principally abstract methods that it catches, since there already is a mechanism for forcing their overriding/implementation. It is misspellings in supposedly well‑known methods that it is intended to catch.Three deliberate errors all of which @Override found in a few seconds, otherwise they would have come back and bitten us.
 
Jesse Silverman
Saloon Keeper
Posts: 1327
40
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: It isn't principally abstract methods that it catches, since there already is a mechanism for forcing their overriding/implementation.



Sure, but if you wanted your class to implements three interfaces, and forgot one when defining it, @Override would remind you of that case as well.
That is, you did provide an implementation for the abstract method from some interface, but never declared your class to implements that interface.
I believe I have done that in the heat of coding!
 
Junilu Lacar
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For OP's benefit, the discussion about using the @Override annotation to help detect mistakes relates to your question because you could have done this:

With the @Override annotation on line 13, the compiler would generate an error to inform you of something wrong.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic