Java Variable Syntax
Variables are data identifiers. Variables are used to refer to specific values that are generated in a program--values that you want to keep around. Program data is often easier to understand and manipulate if each datum has its own name. Instead of naming each of your nine children George, giving each child a unique name makes it much easier to refer to them and to understand documents such as wills and family trees. The same concept holds true for variables in a program. Giving variables meaningful names makes source code easier to read and understand.
Java has three kinds of variables: local (automatic), instance, and class. Local variables are declared within a method body. Both instance and class variables are declared inside a class body but outside of any method bodies. Instance and class variables look similar, except class variables are prepended with the static modifier.
Let's take a look at the general form for declaring each kind of variable in a Java class. The following class is not real Java code; it is only pseudocode:
class someClass { // Instance Variable visibility_modifier variable_type instanceVariableName; // Class Variable visibility_modifier static variable_type classVariableName; returnType someMethod() { // Local Variable variable_type localVariableName; } }
The pseudocode above contains three variable declarations. The variable names are instanceVariableName, classVariableName, and localVariableName. Variable names must be legal Java identifiers.
Each of the three kinds of variables has a different scope. A variable's scope defines how long that variable lives in the program's memory and when it expires. Once a variable goes out of scope, its memory space is marked for garbage collection by the JVM and the variable may no longer be referenced.
Notice that both instanceVariableName and classVariableName are declared within someClass but outside of any of its methods (only one method exists in this example). localVariableName, on the other hand, is declared within someMethod. Local variables must be declared within methods. This includes being declared within code blocks nested inside of a method--within an if, while, or for block, for instance. A variable is local to the code block within which it is declared. Being declared within a method body makes a variable local to that method. Being declared within an if block makes the variable local to that if block, for example. This means that the variable cannot be used outside of the curly braces within which it was declared. In fact, it can only be used from the line it is declared on until the code block ends.
Variable Type
Each of the three variables in our general form example also has a variable_type modifier. variable_type defines the type of data that the variable holds. It must be either a Java primitive (int, boolean, etc.) or a Java class (String, Object, MyModel, etc.). Java is a strongly typed language, so variables can only hold data that is of the correct type.
Primitives are not full-fledged objects in Java. They are treated as lightweight containers for storing and accessing a value. Since they are not objects, they cannot be used where an object is required. For instance, storing primitive values in a java.util.Collection requires the primitive be wrapped in a special wrapper class such as java.lang.Integer or java.lang.Double. Each primitive in Java has a corresponding wrapper class.
Object references are variables that refer to Java objects. Instead of holding a primitive value, they hold the memory address of the object. The object may be of a user-defined class or it may be one of the classes included in the Java APIs such as java.lang.String or java.util.List.
Here are all the possible variable types with their memory requirement and default values:
| Java Variables | |||
|---|---|---|---|
| Type | Size in Bits | Initialization | |
| boolean | 1 | false | |
| char | 16 | '\uoooo' | |
| byte | 8 | 0 | |
| short | 16 | 0 | |
| int | 32 | 0 | |
| long | 64 | 0L | |
| float | 32 | 0.0F | |
| double | 64 | 0.0 | |
| Object | n/a | null | |
Variable Visibility
The visibility (also know as access scope) of instance and class variables defines what objects can read and modify that variable. The four possible visibility modifiers are: public, protected, private, and no modifier. Confusing, huh? If no visibility modifier is included with an instance or class variable, then it is said to have default visibility. You will also hear it called package-private and package-protected. As a general rule you should make your instance and class variables private unless you have reason to make them more visible to other classes. Keeping an object's data hidden is called encapsulation and is one of the tenents of good object-oriented development.
Here is a break down of how each modifier affects access to a variable. They are listed from least restrictive to most restrictive:
| Variable Visibility | |||
|---|---|---|---|
| Modifier | Can be Accessed By | ||
| public | any class | ||
| protected | owning class, any subclass, any class in the same package | ||
| no modifier | owning class, any class in the same package | ||
| private | owning class | ||
Other Variable Modifiers
In addition to what is shown here, variables can also have the transient, volatile, and final modifiers. These will be covered in later tutorials.
In the next part of this tutorial we'll take a look at how variables are assigned values. Then we'll put it all together in to a Java program demo.