Questions tagged [constructor-reference]

Beginning with Java 8, a constructor reference is a special type of method reference. Use this tag for question specific to constructor reference, not for general method reference.

Method reference were introduced in Java 8 with the new operator ::. They are defined in the JLS in section 15.13.

Constructor reference are method reference for constructors. They are written as:

ClassType :: [TypeArguments] new
ArrayType :: new

They comply with the Supplier interface:

Supplier<Test> testTupplier = Test::new // same as lambda expression () -> new Test()
13 questions
145
votes
1 answer

Are there constructor references in Kotlin?

In Java we have the Class::new syntax for constructor references. I know, there are callable references for methods, but how about constructors? A typical use case for me would be factories.
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
63
votes
5 answers

Runnable::new vs new Runnable()

Why doesn't the first of the following examples work? run(R::new); method R.run is not called. run(new R()); method R.run is called. Both examples are compiled-able. public class ConstructorRefVsNew { public static void main(String[] args) { …
user1722245
  • 2,065
  • 1
  • 18
  • 31
29
votes
2 answers

Invalid constructor reference when using local class?

Given the following code: package com.gmail.oksandum.test; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { } public void foo() { class LocalFoo { …
Rocoty
  • 406
  • 5
  • 11
20
votes
3 answers

Can Java 8 implement interface on the fly for method reference?

I learn new features of Java 8. I am playing with different examples and I have found a strange behaviour: public static void main(String[] args) { method(Test::new); } static class Test{ } private static void method(Supplier
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
16
votes
1 answer

Constructor reference for inner class fails with VerifyError at runtime

I am creating a supplier for an inner class constructor using the lambda ctx -> new SpectatorSwitcher(ctx). IntelliJ suggested that I change it to SpectatorSwitcher::new instead. SpectatorSwitcher is a non-static inner class of the class I'm working…
yawkat
  • 374
  • 2
  • 14
9
votes
1 answer

Java 8 constructor method references

I am reading Java 8 book, and it comes with a sample I reproduce: @FunctionalInterface public interface Action { public void perform(); } An Implementor: public final class ActionImpl implements Action { public ActionImpl() { …
chiperortiz
  • 4,751
  • 9
  • 45
  • 79
7
votes
1 answer

How to pass a type parameter to a generic class constructor reference?

Assume the following code: class ConstructMe {} data class Test constructor(var supplier: () -> ConstructMe) {} fun main(args: Array) { works() breaks() } fun works() { Test({ ConstructMe() }) //…
5
votes
2 answers

Why doesn't it output 'dog eat'

code like this public class LambdaTest { public static void main(String[] args) { final Animal animal = Dog::new; animal.eat(); } } @FunctionalInterface interface Animal { void eat(); } class Dog implements Animal { …
vozrr
  • 71
  • 2
5
votes
1 answer

Automatic constructor matching in default method

I have a PersonFactory interface as follows: @FunctionalInterface public interface PersonFactory

{ P create(String firstname, String lastname); // Return a person with no args default P create() { // Is there a…

4
votes
1 answer

How to reference parameterized constructor in Kotlin?

So I have this definition: sealed interface ParseResult { data class Success(val value: R) : ParseResult data class Failure(val original: String, val error: Throwable) : ParseResult } I want to wrap…
caeus
  • 3,084
  • 1
  • 22
  • 36
3
votes
2 answers

What is the purpose of constructor references in Kotlin

I am reading the book Kotlin in action and I ask myself what is the purpose of "creating an instance of a class using a constructor reference" (page 112 if anyone is interested and has the book at home). Here is the code example from the book: data…
Torben G
  • 750
  • 2
  • 11
  • 33
1
vote
1 answer

How do I use [TypeArguments] with a constructor reference in Java 8?

Section 15.13 of the Java Language Specification for Java 8 describes this form of the method reference syntax for creating a constructor reference: ClassType :: [TypeArguments] new For example: String s = "abc"; UnaryOperator
skomisa
  • 16,436
  • 7
  • 61
  • 102
-2
votes
1 answer

How to create object of generic type in a class

I have the following class open abstract class NexusAdapter> (protected val ctx: Context, private val _layoutId: Int, protected val items: List): …
Simple Fellow
  • 4,315
  • 2
  • 31
  • 44