Local Variables in Java: Declaration & Examples

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java lets us create variables that are kept safe from other parts of the program. This lesson will describe how to declare these variable types, and provide examples of their use.

Declaring Local Variables

In Java, there are many places to declare variables. You can declare them at the start of the program, within the main method, inside classes, and inside methods or functions. Depending on where they are defined, other parts of your code may or may not be able to access them.

A local variable is one that is declared within a method. It cannot be used outside of that method. It is like a gift card to a specific store. It only works inside that store.

Let's say we have a class for a planetary system. Within that class, there is a method to calculate distance in kilometers.

public class PlanetarySystem {
  public double calcDistance(double parsecs) {
   //local variable km
   double km = parsecs * 30856775813057.62;
   return km;

Although this method returns the value back to whoever calls the method, you cannot access it from any other method.

The following code sends the calcDistance method a parsec value.

public static void main(String[] args) {
  double parsecs;
  PlanetarySystem coruscant = new PlanetarySystem();
  System.out.println("Enter parsecs: ");
  Scanner enterkm = new Scanner(;
  parsecs = enterkm.nextDouble();
  System.out.println("Total KM to Coruscant = " + coruscant.calcDistance(parsecs));

And the output:

Java call local var output

Even though we were able to see the value of the variable km, we are unable to access it within the main function. If the PlanetarySystem class had other methods, NONE of them would be able to access the km variable within the calcDistance method.

However, you could create a dozen local variables, in a dozen different methods, each with the same name and data type. Confusing? Yes, but think of simple variables, such as i. We use i in counters and loops it's probably OK to use it over and over. Plus, by having it as a local variable, the counter remains isolated within that method!

If you created a global variable i, and used it in every single for loop, there will be instances where the variable will not have the value you expect. This can cause serious problems in your code by creating infinite loops, crashing the program, or updating the wrong information. If you're expecting the code to be updating Employee #729 with a pay rate of $150/hr but it's really on Employee #17, this would be a very costly mistake.

Reducing Scope of Local Variables

If we talk about reducing local variable scope, you may quickly point out that local variables are already limited in scope!

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account