7.2. Method Declarations

General syntax of the method declaration is as follows:

<method modifiers> <return type> <method name>(<formal parameter list>) <throws clause> // Method header
{ // Method body
<local variable declarations>
<nested local class declarations>
<statements>
}

The above method declaration syntax can be divided into

Let’s add couple of methods to the CoffeeMaker class we created earlier to look at some of the elements in method header and method body. Start looking for changes made to Coffeemaker class from comment Line // 3. Identify parts of Method header

Note: throws clause and nested declarations are covered in coming chapters.

			package javatutorial;

			/**
			 * Class to understand/identify the following elements in class declaration
			 *     •	Class header
			 *     		o	class modifiers  can be public, private, protected etc
			 *     		o	class the Java keyword
			 *     		o	class name name of the Class
			 *     
			 *     •	Class body
			 *     		o	field declarations  declaration of any fields in a class
			 *     		o	method declarations declaration of any methods in a class
			 *     		o	constructor declarations constructor declarations, if any
			 * 
			 * Class to understand/identify the following elements in method declaration
			 *
			 *    •	Method header
			 *    	o	method modifiers
			 *    	o	return type
			 *    	o	method name
			 *    	o	formal parameter list
			 *    
			 *    •	Method body
			 *    	o	local variable declarations
			 *    	o	statements
			 *    
			 *  @author f5java.com   
			 */
			public class CoffeeMaker {
				// 1. Identify parts of class header
				
				// class modifier is public
				// class is the java keyword
				// class name is CoffeeMaker
				
				// 2. Class body consists of
				
				boolean isCoffeemakerOn; // 2a. Field declarations
				int noOfCups;
				String brandName;
				String statusTracker = "Started making your coffee";
				
				/**
				 * Prints coffee making status
				 */
				public void displayCoffeeMakerStatus(){ // 2b. Method declaration
					System.out.println("Coffee make is on: " + isCoffeemakerOn);
					System.out.println("Number of cups requested: " + noOfCups);
					System.out.println("Your coffee making is " + statusTracker);
				}
				
				// 2c. Constructor declaration
				CoffeeMaker(boolean machineSwitch, int requestedQty, String brand){ 
					this.isCoffeemakerOn = machineSwitch;
					this.brandName = brand;
					this.noOfCups = requestedQty;
				}
				
				// 3. Identify parts of method header in areYouMakingCoffee method
				
				// method modifier is public
				// return type of the method is boolean
				// method name is areYouMakingCoffee
				// formal parameters are typeOfCoffee and noOfCups
				
				public boolean areYouMakingCoffee(String typeOfCoffee, int noOfCups){
					// 4. Method body consists of
					boolean status = false; // 4a. local variable
					if(noOfCups < 5 ){
						status = true; // 4b. statements - assignment statement in this case			
					}		
					return status;		
				}

				public static void main(String[] args) {
					// Instantiate and call the method
					CoffeeMaker myCoffee = new CoffeeMaker(true, 2, "StarBucks");
					myCoffee.displayCoffeeMakerStatus();
					System.out.println("Are you still making Coffee" + myCoffee.areYouMakingCoffee("starbucks", 2)) ;
				}

			}