Java constructor reference assignment Vs new created object assignment -
i met in our project following code:
myinterface var = myclass::new; is there difference with
myinterface var = new myclass(); lazy?
myinterface var = new myclass(); creates instance of myclass , assigns variable of type myinterface. requires myclass implements myinterface , have no-arg constructor. result instance of myclass implements myinterface likes to.
myinterface var = myclass::new; attemps implement myinterface ad-hoc. requires myinterface in functional interface having single abstract method. single abstract method must have return type assignable myclass , parameter list matching 1 of myclass’ constructors.
it analog of:
myinterface var = new myinterface() { public myclass anymethodname() { return new myclass(); } the result instance of myinterface on invocations of single abstract method create new instance of myclass passing of arguments constructor of myclass.
in other words, these 2 constructs have nothing in common.
Comments
Post a Comment