Java Reference
In-Depth Information
To create a variable, you must give it a name and identify what type of information it
will store. You also can give a variable an initial value at the same time you create it.
There are three kinds of variables in Java: instance variables, class variables, and local
variables .
Instance variables , as you learned yesterday, are used to define an object's attributes.
Class variables define the attributes of an entire class of objects and apply to all
instances of it.
Local variables are used inside method definitions or even smaller blocks of statements
within a method. You can use them only while the method or block is being executed by
the Java interpreter. They cease to exist afterward.
2
Although all three kinds of variables are created in much the same way, class and
instance variables are used in a different manner than local variables. You will learn
about local variables today and explore instance and class variables during Day 3,
“Working with Objects.”
NOTE
Unlike other languages, Java does not have global variables , vari-
ables that can be used in all parts of a program. Instance and
class variables communicate information from one object to
another, so they replace the need for global variables.
Creating Variables
Before you can use a variable in a Java program, you must create the variable by declar-
ing its name and the type of information it will store. The type of information is listed
first, followed by the name of the variable. The following are all examples of variable
declarations:
int loanLength;
String message;
boolean gameOver;
NOTE
You learn about variable data types later today. In these examples,
the int type represents integers, String is an object that holds
text, and boolean is used for Boolean true/false values.
 
Search WWH ::




Custom Search