2.7. Invoking Methods

In Java Objects communicate by message passing. This is done by calling a method on the object using the binary infix dot ('.') operator.

A method call consists of the following

First, let us modify the Employee class by adding a method which helps in covering all aspects of methos invocation. Remember, more you type in these examples and excute the programs on your own the better java developer you become.

			package javatutorial;

			/**
			 * Example of a class, modified to demonstrate the concepts in method invocation 
			 * 
			 * @author f5java.com
			 */
			public class Employee {
				// Fields : value in fields represent the state of the class
			   private String empName;
			   private int age;
			   private String designation;
			   private double salary;
			   private String projectAssigned;	   
			   
			   // Methods: represent the behavior of the class
			   
			   /**
				* Displays employee details 
				*/
			   public void displayEmployeeDetails(){
				   System.out.println("Employee Name: " + empName);
				   System.out.println("Employee Age: " + age);
				   System.out.println("Employee Designation : " + designation);
				   System.out.println("Employee Salary : " + salary);
				   System.out.println("Employee Project : " + projectAssigned);
			   }
			   
			   public void setEmployeeDetails(String empName,int age,String designation, 
													double salary,String projectAssigned){
				   this.empName = empName;
				   this.age = age;
				   this.designation = designation;
				   this.salary = salary;
				   this.projectAssigned = projectAssigned;
				   
			   }
			   
			   public String getEmployeeName(){
				   return empName;
			   }
			   
			   public int getEmployeeAge(){
				   return this.age;
			   }

			}
		

We've been invoking methods all along in this java tutorial, however lets take a deep dive into method invocation, As an example, we'll invoke methods on the Employee class we created earlier.

			package javatutorial;

			/**
			 * Class to understand the following concepts invoking methods
			 *     Object: on which method has to called
			 *     Method Name: the method to be invoked on the object.
			 *     Arguments: arguments to the method, if any.
			 *     Return Value: return value from the method call, if any.
			 *     
			 *  @author f5java.com   
			 */
			public class InvokeMethods {

				public static void main(String[] args) {
					
					// 1. employee is the object on which method has to be invoked
					Employee employee = new Employee();
					
					// 2. setEmployeeDetails is the method name in employee object
					// 3. Arguments passed are empName, age, designation, salary, projectAssigned
					employee.setEmployeeDetails("Bob", 35, "Java developer", 80000, "f5java.com");
					
					// 4. Return Value: the value coming from employee.getEmployeeName() method call
					//                  in this case, its employee name
					
					//5. Return value: value from employee.getEmployeeAge()
					System.out.println("Employee's name is " + employee.getEmployeeName());
					System.out.println("Employee's age is " + employee.getEmployeeAge());
				}

			}