Java Variable Types: Static, Instance & Local

Instructor: Benjamin Blanchard

Ben has taught ESL and web programming and has a M.S. in education.

Broadly speaking, there are three types of variables in Java. Learn about scope and how to use these different types of variables by looking at a simple example with a bit of code.

Where Can Variables Be Seen and Why Limit Visibility?

In Java, it's important to understand which variables you can access in different parts of the code. This is often referred to as scope because it determines which variables can be 'seen' in a given part of the code. Sometimes you'll want to only set a variable to a value for a specific operation. It wouldn't make sense to keep the value around after the operation has been completed. Also, depending on what you've named it, it might come in conflict with a variable that has the same name in another part of the code. Variable scope is a way to keep things in their place.

Man looking through a telescope

The Three Types of Variables

There are three basic levels of scope in Java: local, instance and static.

Local Variables - Temporary Storage

A local variable is one that exists inside a method or a block of code (a grouping of statements enclosed by curly braces), and exists only for as long as that method or block is executing. Once the program reaches the end of the method (or block), the local variable disappears from memory. The next time the method is called (or the block is run) a completely new version of the local variable comes into existence. One of the most common types of local variables is a parameter, that is, a named value that is 'sent in' when a method is called.

Instance Variables - Staying with the Object

Now on to instance variables. To create an instance variable, you declare it between the curly braces defining a class, but outside any methods or blocks. An instance variable is associated with the class in which it is defined. In fact, instance variables are sometimes referred to as class members because they are often used to store attributes of a particular object. For example, we could create many different balloon 'objects' that would all have a color, but we'd want to remember the specific color of each balloon. Instance variables come into existence when an object is created, and continue to occupy space in memory until that object no longer exists.

Static Variables - Storage for the Life of the Program

Finally, Java has static variables. Static variable are declared in the same place as instance variables, but with the keyword 'static' before the data type. While instance variables hold values that are associated with an individual object, static variables' values are associated with the class as a whole. Because of this, they are sometimes referred to as 'class variables.' They're called static because they stay the same for all objects created from the class in which they are defined. Often, static variables are used to define constants associated with the class of objects, but if a static variable is changed, the change is reflected for all objects.

Variable Types in Action

So let's look at some concrete examples of how these different variable types can be used.

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