|
byte |
|
short |
|
int |
|
long |
|
float |
|
double |
|
char |
|
boolean |
|
Class Types (see book or documentation for details)
String Example of Declarations: String Name; Name = "Lynne Grewe"; OR String Name = new String("Lynne Grewe"); OR String Name = "Lynne Grewe"; |
Boolean |
Double |
Float |
Short |
Byte |
Long |
Variables can be declared with a protection type or status that restricts
who can access them. See the page on Protection
Types for more information.
Exercise
If a variable is declared static it means that the value of this variable will be the same for any object instatiated from its class. For example,
class Document { static int version=10; ..... }
This means that any object instatiated from Document will have
the same value of version. Whenevery any Document object
changes the value of version it is changed for all instances of
this object. This is somewhat like the concept of a global
variable.
You can access a static variable through the class name
or an instance of the class:
Document.version++; Document d = new Document(); d.version = 7;
This is akin to a constant in that it is set once and can never be changed. The value is set at declaration time as shown in the example below.
class Document { final int year 1997; ..... }