Skip to content

Local Variables

Parameters are variables that hold values for the data a caller passes.

Inside a method, we can declare other variables, called "local" variables.

local variable

A variable declared within a method.

Variables declared inside a method are called "local" because their scope is limited to the method. * This means the variables cannot be used outside the method's curly braces { }.

Local Variable Scope

Practice Exercise

A local variable must be initialized before you can use it.

A parameter is like a local variable because its scope is also the method's curly braces.

Drill

Methods/src/drills/StringManipulation.java * Complete items 1 - 3 according to the directions.

(Solution: StringManipulation.java)

  • (Optional) Add a third parameter String charToAdd to addCharacter. Concatenate this character instead of !. In main, prompt the user for a character to add, and pass the String to addCharacter.

(Solution: StringManipulation2.java)


Prev -- Up -- Next