Interview program on String using toString() method

In this article, we will discuss a sample program on String which is commonly asked to test skill on overridden toString() method

 

What will be output of below program?

There are 2 classes viz.,;

  • Employee POJO
  • Employee main, to begin execution

 

Employee.java

package in.bench.resources.interview.programs;

public class Employee {

	// member variables
	String name;
	String address;

	// getter & setter
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}

	// override toString() method
	@Override
	public String toString() {
		return "Employee [name=" + name + ", address=" + address + "]";
	}
}

 

EmployeeMain.java

package in.bench.resources.interview.programs;

public class EmployeeMain {

	public static void main(String[] args) {

		// creating employee object
		Employee employee1 = new Employee();

		// update employee info
		updateEmployee(employee1);

		// printing employee information
		System.out.println(employee1);
	}

	// employee update operation
	public static void updateEmployee(Employee employee) {
		employee.setAddress("Address 1");
	}
}

 

 

Output:

Employee [name=null, address=Address 1]

 

Happy Coding !!
Happy Learning !!