Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method reference to local class constructor

There are several similar questions on SO about method reference to local class constructor, but I'd like to clarify slightly other thing. Consider following piece of code:

static Callable gen(int i) {
    class X {
        int x = i;
        public String toString() { return "" + x; }
    }
    return X::new;
}

...

System.out.println(gen(0).call()); 
System.out.println(gen(1).call());

Obviously this will printout

0
1

It turns out, that X class has constructor of the form ...$X(int) (you can find it via X.class.getDeclaredConstructors()).

But what is interesting here, is that returned lambdas (or method references) aren't simple reference to constructor ...$X(int) like, for example, Integer::new. They internally invoke this constructor ...$X(int) with predefined argument (0 or 1).

So, I'm not sure, but looks like this kind of method reference is not precisely described in JLS. And there is not other way except this case for local classes, to produce such kind of lambdas (with predefined constructor arguments). Who can help clarify this?

To be precise:

  • where is in JLS such kind of method reference described?

  • is any other way to create such method reference to arbitrary class constructor with predefined arguments?

like image 519
Andremoniy Avatar asked Jan 18 '16 20:01

Andremoniy


People also ask

How do you access a Java class constructor using method reference?

A method reference can also be applicable to constructors in Java 8. A constructor reference can be created using the class name and a new keyword. The constructor reference can be assigned to any functional interface reference that defines a method compatible with the constructor.

What is method Referance in Java?

Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.

Which of the following is an example of method reference?

To make the code clear and compact, In the above example, one can turn lambda expression into a method reference: list. forEach(System. out::println);

What is the difference between a method reference and a lambda expression?

Lambda expression is an anonymous method (method without a name) that has used to provide the inline implementation of a method defined by the functional interface while a method reference is similar to a lambda expression that refers a method without executing it.


1 Answers

You are focusing too much on irrelevant low level details. On the byte code level, there might be a constructor accepting an int parameter, but on the language level, you didn’t specify an explicit constructor, hence, there will be a default constructor without any arguments, as with any other class.

This should become clear when you write the pre-Java 8 code:

static Callable<Object> gen(int i) {
    class X {
        int x = i;
        public String toString() { return "" + x; }
    }
    X x=new X();
    …

You instantiate X by its default constructor, not taking any arguments. Your local class captures the value of i, but how it does so on the low level, i.e. that X’ constructor has a synthetic int parameter and the new expression will pass the value of i to it, is an implementation detail.

You can even add an explicit constructor as

        X() {}

without changing anything.

Obviously, you can also write the expression new X() inside a lambda expression here, as expressions don’t change their semantic when being placed inside a lambda expression:

    return () -> new X();

or use it’s short-hand form, the method reference

    return X::new;

There is nothing special about it, the behavior is understandable even without referring to the specification, if you forget about the distracting low level details. X may capture as many local variables as you like, the constructor’s number of parameters doesn’t change (on the language level).

like image 160
Holger Avatar answered Nov 15 '22 00:11

Holger