Constructor method reference for (non-static) inner class?

billc.cn :

Is there something equivalent for StaticClass::new for inner class given the outer class instance?

Edit:

I.e. if I have

class Outer {
    class Inner {
    }
}

I can do Outer o = new Outer(); Inner i = o.new Inner() in old Java. How can I express the o.new Inner() as function reference.

Konstantin Yovkov :

According to the Oracle tutorials, there are four kinds of method references:

  • Reference to a static method
    • ContainingClass::staticMethodName
  • Reference to an instance method of a particular object
    • containingObject::instanceMethodName
  • Reference to an instance method of an arbitrary object of a particular type
    • ContainingType::methodName
  • Reference to a constructor
    • ClassName::new

References to a local/nested class are not listed, so I would assume it's not supported.

You can use the java.util.function.Supplier to trigger the usage of lambdas in order to obtain an instance of the nested class:

Outer outer = new Outer();
Supplier<Outer.Inner> supplier = () -> outer.new Inner();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Non static method reference in enum constructor as parameter

From Java

How to instantiate non static inner class within a static method?

From Dev

Call a non-static method from static inner class

From Dev

Making a static reference to a non-static method in another class

From Dev

Is constructor a static method of a class?

From Java

Why can’t this static inner class call a non-static method on its outer class?

From Dev

Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

From Java

Workaround to accessing non-static member method from a static inner class

From Java

Why can't we have static method in a (non-static) inner class?

From Java

Constructor Inside Inner Static Class in java?

From Java

Method reference to local class constructor

From Dev

Method reference to anonymous class constructor

From Dev

Static reference to a non-static method

From Dev

Method reference static vs non static

From Dev

Static reference to non-static method

From Dev

Instantiation of a Non-static Class(context) in a Static method with a Constructor taking type Object as parameter

From Dev

static reference from class to non-static method in data base adapter can't be done

From Dev

Generic constructor reference and static method in TypeScript

From Dev

how to pass in class' non-static method into class' data member's constructor

From Dev

Receive broadcast in MainActivity with non static inner class

From Dev

Reference Static member in Static Method In a Powershell Class

From Java

Main method in a static inner class.?

From Dev

method reference Java using an inner class

From Dev

Spring java config to invoke a public non static method of a singleton class with private constructor

From Dev

java 8 method reference calling non static method in a static way

From Java

Java 8: method reference to a static method in a non-static way

From Java

Inner class allows static fields and non constant static expression - Why?

From Java

Referencing non static variable from within static Inner Class

From Dev

Compile error at static final object inside non static inner class

Related Related

  1. 1

    Non static method reference in enum constructor as parameter

  2. 2

    How to instantiate non static inner class within a static method?

  3. 3

    Call a non-static method from static inner class

  4. 4

    Making a static reference to a non-static method in another class

  5. 5

    Is constructor a static method of a class?

  6. 6

    Why can’t this static inner class call a non-static method on its outer class?

  7. 7

    Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

  8. 8

    Workaround to accessing non-static member method from a static inner class

  9. 9

    Why can't we have static method in a (non-static) inner class?

  10. 10

    Constructor Inside Inner Static Class in java?

  11. 11

    Method reference to local class constructor

  12. 12

    Method reference to anonymous class constructor

  13. 13

    Static reference to a non-static method

  14. 14

    Method reference static vs non static

  15. 15

    Static reference to non-static method

  16. 16

    Instantiation of a Non-static Class(context) in a Static method with a Constructor taking type Object as parameter

  17. 17

    static reference from class to non-static method in data base adapter can't be done

  18. 18

    Generic constructor reference and static method in TypeScript

  19. 19

    how to pass in class' non-static method into class' data member's constructor

  20. 20

    Receive broadcast in MainActivity with non static inner class

  21. 21

    Reference Static member in Static Method In a Powershell Class

  22. 22

    Main method in a static inner class.?

  23. 23

    method reference Java using an inner class

  24. 24

    Spring java config to invoke a public non static method of a singleton class with private constructor

  25. 25

    java 8 method reference calling non static method in a static way

  26. 26

    Java 8: method reference to a static method in a non-static way

  27. 27

    Inner class allows static fields and non constant static expression - Why?

  28. 28

    Referencing non static variable from within static Inner Class

  29. 29

    Compile error at static final object inside non static inner class

HotTag

Archive