Method References and Constructor References in Java 8

Method references or constructor references can be used to refer to an existing method or constructor by name. Classes containing these methods can be regular classes without the need to implement or extend anything. However, the target type needs to be a functional interface, as the signature of the method is infered from the functional interface's abstract method.

Target type is the type to which it is assigned through an equal operator (=) or the type of a method parameter to which it is passes to. Without a target type Java will not be able to infer the type of the method or constructor reference. This also means that there should be one functional interface per method or constructor. 

Method references or constructor references are not invoked when they are assigned to a target type. They need to be invoked later using the reference type.


Method References

You can use method references on :

  • Static methods of any class

  • Instance methods of a particular object

  • Instance methods of an arbitrary object

  • References to constructor methods (constructor references)


Example: static method reference

Class Employee{

  public static int compareAge( Employee e1, Employee e2)


    // compare e1 and e2 and return return an integer

    // according to the contract of compare method of comparator.


We can now use this static method as:

Collections.sort(empList, Employee :: compareAges);

Sort method is expecting an instance of the comparator interface. Comparator interface has a single abstract method that accept two values  and returning a data type that can be used by the sort method. We are also passing a method that accept two values and return a datatype that can be used for sorting.


Example: Instance method reference

We can refer to an instance method in same class as:

Collections.sort (empList,this::compareAges);


Constructor Reference

There should be one functional interface per constructor.

Constructor references provide a means to give a different name to different constructors within a class. Otherwise they all will be having the same name as that of the class. 



Give examples for:

  1. Refering to an instance method of another object.

  2. Constructor reference.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)