.

MathBits.com

   Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use  

   Methods -- Style 2 

Style 2:  Our second style of method will take arguments (parameters) but will not return a value.  The parameter list in the parentheses specifies the types and number of arguments that are passed to the method. 

 

    

//Demo Program for Methods

import java.io.*;
import BreezyGUI.*;

public class DisplayClass
{
      public static void main (String[ ] args)
     {
        greeting(5);  // Method call with actual argument 5     
           int number;
           do
    
      {
                 number = Console.readInt("\nPlease enter value(1-10): ");
            }
           while ((number < 1) || (number > 10));
           greeting(number);
//Method call again with a variable argument
      }

  
  // Method for greeting

     public static void greeting(int x)   //parameter shows need for int
     {
          int i;   //
declaring LOCAL variable*
          for(i = 0; i < x; i++)
          {
               System.out.print("Hi ");
          }
          System.out.println( );
    }
}  

Output:
Hi Hi Hi Hi Hi

Please enter value(1-10): 4
Hi Hi Hi Hi

*  A local variable is a variable that is used only within the method. Such a variable is NOT recognized by main.  When execution returns to main, the local variable will no longer be available for use.

                     
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use