• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

::new in Java

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main( String[] args ) {
2
Defaulable defaulable = DefaulableFactory.create( DefaultableImpl::new );
3
System.out.println( defaulable.notRequired() );
4

5
defaulable = DefaulableFactory.create( OverridableImpl::new );
6
System.out.println( defaulable.notRequired() );
7
}

I recently came across the above code in one of the article. Though I understand that ::new is used to pass the instance of the class. I would love to really read and understand about ::new. Can someone has any article links about it?
 
Nandhini Sridharan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay this is basically as part of method reference. While DefaultableImpl::new refers to constructor reference of DefaultableImpl class.

Thanks.
 
Marshal
Posts: 79401
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is like writing new Foo. In a λ you can write Foo::new instead.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on the functional interface used for DefaulableFactory.create, DefaultableImpl::new can mean one of several things:

- If create takes a Supplier or Callable, it means () -> new DefaultableImpl().
- If create takes a Function (IntFunction, LongFunction, DoubleFunction), it means t -> new DefaultableImpl(t).
- If create takes a BiFunction, it means (t, u) -> new DefaultableImpl(t, u).

There are more out-of-the-box functional interfaces, and there can be even more, but you should get the picture.
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

If it helps

Here is the link to this part in Java Tutorial

https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've found this link to be a good one for what you're looking for. http://winterbe.com/posts/2014/03/16/java-8-tutorial/
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic