Local Variables

5.4 Local Variables

Variables declared within a method, become local to the method. Local variables have local scope: They can only be accessed within the function. Local variables are created when a method starts, and deleted when the method is completed.
import java.util.Scanner;    // Needed for the Scanner class

/**
 * This program demonstrate local
 * variable of a method.
 */
public class LocalVariable
{
   public static void main(String[] args)
   {
      // Create a Scanner object for keyboard input.
      Scanner console = new Scanner(System.in);
      int     number1, number2; //Hold numbers

      //Get number from users
      System.out.print("Enter first integer : ");
      number1 = console.nextInt();
      System.out.print("Enter second integer : ");
      number2 = console.nextInt();
      
      //Call method to display sum
      sum(number1, number2);
   }

   /**
    *  The sum method accept two arguments.
    */
   public static void sum(int num1, int num2)
   {
      int total; // local variable stores intermediate results.

      total = num1 + num2;
      System.out.println("Total : " + total);
   }
}
Output :
Enter first integer : 34
Enter second integer : 31
Total : 65


No comments:

Post a Comment

Pages