JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Method Reference - Reference to a Constructor example.


This page shows a simple example of Method Reference feature of reference to a construtor:

package com.java2novice.methodreference;

import com.java2novice.lambda.Employee;

public class ConstructorRefEx {

	public static void main(String a[]) {

		EmployeeFactory empFactory = Employee::new;
		Employee emp = empFactory.getEmployee("Nataraja G", "Accounts", 8000);
		System.out.println(emp);
	}
}

Here is EmployeeFactory class:

package com.java2novice.methodreference;

import com.java2novice.lambda.Employee;

public interface EmployeeFactory {

	public abstract Employee getEmployee(String name, String account, Integer salary);
}

Here is Employee POJO class:

package com.java2novice.lambda;

public class Employee {

	private String name;
	private String account;
	private Integer salary;

	public Employee(String name, String account, Integer salary) {
		super();
		this.name = name;
		this.account = account;
		this.salary = salary;
	}

	@Override
	public String toString() {

		return "name: "+ this.name +" | account: "+ this.account +" | salary: "+this.salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public Integer getSalary() {
		return salary;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}
}

Output:
name: Nataraja G | account: Accounts | salary: 8000
<< Previous Program 

Java-8 Method References Examples

  1. Method Reference - Reference to a Static Method example.
  2. Method Reference - Reference to a Instance Method example.
  3. Method Reference - Reference to a Constructor example.
Knowledge Centre
ServletOuptputStream Vs PrintWriter
ServletOutputStream: ServletResponse.getOutputStream() returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data, it sends the raw data as it is.

PrintWriter: ServletResponse.getWriter() returns PrintWriter object which sends character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified then it does default character encoding.
Famous Quotations
Tomorrow is often the busiest day of the week.
-- Spanish Proverb

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.