Java Variable Scope: Definition & Best Practices

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.

Object-oriented programing lets us keep variables close to the vest, or share them with the world. In this lesson, we'll cover variable scope and provide code examples.

Keeping Variables in Check

As an object-oriented programming language, Java lets us keep a rein on how visible our variables are. We can share them with the world (global variables), or keep them hidden from other objects and functions.

When we talk about managing a variable's visibility or accessibility, we use the term scope. By controlling where a variable is used, we can make sure that one method or function doesn't update a variable that it shouldn't. For example, a common use for an integer variable is a counter. A dozen methods or classes could have a counter; we don't want the bean counter being updated when we are trying to count cards!

Because variables can be contained, you'll see the same variable name used in multiple classes and methods. The most common is probably i, as it's used as a counter in many operations. Let's take a little deeper dive at scope.

Example:

Think of taking clothes to the dry-cleaner.

An instance variable is defined in classes. A method within that class, or a method that has access to the class, can update the variable.

Best Practice

Keep scope as narrow as possible. We'll get into some detail further on, but this should be the key message. If you start with a restricted scope, it's easier to open things up later. It isn't so easy to un-bake the bread once you've exposed a variable to the world.

Let's look at a real-world example and translate it into Java-speak. Think of a dry-cleaning service. The service has several classes and methods. These would include a payment mechanism, ironing/pressing, cleaning, sorting, inventory, etc.

As we progress through the nuances of scope, we will use this as our example. In Java, there are several types of scope: class level, method, and loop.

Class Scope

Many of the variables you use in Java are available within a single Java class.


Java var scope class


NOTE: if a class inherits from another class, it will inherit those variables! If our dry-cleaner has a SortLinen class that inherits from Sort, any variables in the Sort class can be used by the SortLinen class.

This is very important because you can create a family of classes, with a patriarch at the top. In this class, you can declare variables; they will be available to all children of that class!

Method Scope

Declaring a variable within a method further restricts its scope. If you declare a variable within a method, there's no way it can be accessed from other methods. This type of variable is considered a method variable.


Java method variable


In the above example, the variable tempRate is a method variable, since it is declared within the setRates method. You cannot directly access this variable from any other methods. Again, the best practice is to define the variable at the start of the method code; it makes the code more readable.

Although we can't access the tempRate variable in other methods, notice the return statement: We can return the value of the variable to another method or class. When it is returned to another method or class, it is sent as a parameter variable.

Let's take a look at how this works, modifying our original code.


Java parameter variable


The setRates function now takes in a parameter, the rate. This is a parameter variable and it is restricted to this method call. Again, we create a method variable so we can do some work with the incoming rate.

To unlock this lesson you must be a Study.com 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 Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
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? Study.com 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
Support