Local Variables in Method main

Line 10 declares and initializes Scanner variable input, which is used to read values entered by the user. Lines 13, 14, 20 and 26 declare local variables total, gradeCounter, grade and average, respectively, to be of type int. Variable grade stores the user input.

These declarations appear in the body of method main. Recall that variables declared in a method body are local variables and can be used only from the line of their declaration to the closing right brace of the method declaration. A local variable’s declaration must appear before the variable is used in that method. A local variable cannot be accessed outside the method in which it’s declared. Variable grade, declared in the body of the while loop, ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.