Skip to main content
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

import java.util.List;
import java.util.ArrayList;

public class Java8Tester {
   public static void main(String args[]){
      List names = new ArrayList();
  
      names.add("Mahesh");
      names.add("Suresh");
      names.add("Ramesh");
      names.add("Naresh");
      names.add("Kalpesh");
  
      names.forEach(System.out::println);
   }
}
Here we have passed System.out::println method as a static method reference.

Verify the Result

Compile the class using javac compiler as follows −
$javac Java8Tester.java
Now run the Java8Tester as follows −
$java Java8Tester
It should produce the following output −
Mahesh
Suresh
Ramesh
Naresh
Kalpesh

Comments

Popular posts from this blog

In this chapter, we will see how to enable remote desktop application. It is important because this enables us to work remotely on the server. To do this, we have the following two options. For the first option, we have to follow the steps given below. Step 1  − Go to Start → right click “This PC” → Properties. Step 2  − On Left side click “Remote Setting”. Step 3  − Check radio button “Allow Remote connection to this computer” and Check box “Allow connection only from computers running Remote Desktop with Network Level Authentication (recommended)” → click “Select Users”. Step 4  − Click Add. Step 5  − Type user that you want to allow access. In my case, it is administrator → click OK. For the  second option , we need to follow the steps given below. Step 1  − Click on “Server Manage” → Local Server → click on “Enable” or Disable, if it is Disabled.

HaydarOrac

Virtualization is one of the most important technology feature and Microsoft has invested on this and the responsible role is called as Hyper-V. Let us now see how to install the Hyper-V Role, for doing this we should follow the steps given below. Step 1  − To Install DNS role go to “Server Manager” → Manage → Add Roles and Features. Step 2  − Click Next. Step 3  − Select the  Role-based  or  feature-based installation  option → click Next. Step 4  − I will install a Local Hyper-V role as it will Select a server from the server pool → Next. Step 5  − From the Roles lists, check the Hyper-V Server role → click Add Features on the popup windows which show up and then → Next. Step 6  − Click Next. Step 7  − Choose your server’s physical network adapters that will take part in the virtualization → Next. Step 8  − Under  Migration , leave the default settings →  Next . Step 9  − Choose the...