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.

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