Java 8-Java Core1 Comment

Method references help to point to methods by their names. A method reference is described using :: (double colon) symbol. A method reference can be used to point the following types of methods −

  • Static methods
  • Instance methods
  • Constructors using new operator (TreeSet::new)

Method Reference Example

Let’s look into an example of method referencing to get a more clear picture. Write the following program in an code editor and match the results.

Java8Tester.java

Here we have passed System.out::println method as a static method reference.

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Tester as follows −

It should produce the following output −


1 Comment on this article

Add a comment


Warning: stream_socket_client(): unable to connect to http://browscap.org/version (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 1366

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 1004