1. Local variables
Let's have a more serious talk about variables. But this time we won't discuss their internal structure. Instead, we'll focus on how variables interact with the code where they are located.
All variables that are declared inside methods are called local variables. A local variable exists only in the block of code in which it is declared. Or, to be more precise, it exists from the moment it is declared until the end of the block of code in which it is declared.
For simplicity, let's consider an example:
Code | Variable visibility |
---|---|
|
|
Let's talk about accessing local variables one more time. Here is a block of code consisting of curly braces: this could be a method body, the body of a loop, or just a block of code for a conditional statement. A variable declared in a block of code exists until the end of that block of code.
If a variable is declared in the body of the loop, then it will exist only in the body of the loop. It is created and destroyed at every iteration of the loop.
Example:
Code | Variable visibility |
---|---|
|
|
We were able to declare a second local variable named b
only because the first b
variable is not visible in the code block where the second b
variable is declared.
2. Parameters
As we said before, each method can have variables that we call parameters. What about their visibility and lifetime?
It's all straightforward. Parameters are created when execution steps into the method (i.e. when the code of the method starts executing). They are eliminated when the method ends. They are visible throughout the body of the method.
Example:
Code | Variable visibility |
---|---|
|
|
As we said earlier, args
is just a variable whose type is an array of strings. And like all parameters, it is available everywhere within the body of the method. That said, we usually ignore it in our examples.
3. Variables in a class
You will recall from the lessons in Level 1 that a class can have methods and variables. Methods are sometimes called instance methods, and variables — instance variables or fields. These are actually synonyms in Java.
What are the variables (or fields) of a class?
They are variables that are declared not in a method, but in a class.
They can be accessed from any (non-static) method of a class. Roughly speaking, instance variables are variables that are shared by all the methods of a class.
Example:
Code | Variable visibility |
---|---|
|
|
In this example, we have two methods — add()
and remove()
. The add()
method increments the sum
and count
instance variables, and the remove()
method decreases the sum
and count
variables. Both methods work on shared instance variables.
Local variables exist while a method is executing. The instance variables of a class exist within an object of a class as long as that object exists. You'll learn details about objects of a class in the next level.
4. Static variables
Like methods, the variables in a class can be static or non-static. Static methods can only access static variables.
In Level 11, we'll analyze the structure of static variables and methods and you'll understand the reasons for these restrictions.
To make a static variable (class variable), you must write the static
keyword in its declaration.
Static variables are not bound to an object or instance of the class in which they are declared. Instead, they belong to the class itself. That's why they exist even if not a single object of the class has been created. You can refer to them from other classes using a construct like:
ClassName.variableName
Example:
Code | Variable visibility |
---|---|
|
|
In the above example, we created a separate Storage
class, moved the count
and sum
variables into it, and declared them static. Public static variables can be accessed from any method in a program (and not only from a method).
GO TO FULL VERSION