Identifiers

Identifiers are the names provided by the programmer, and can be any length in Java. They must start with a letter, underscore, or dollar sign, and in subsequent positions can also contain digits.

A letter that can be used for a Java identifier doesn't just mean uppercase and lowercase A–Z. It means any of the tens of thousands of Unicode letters from any of the major languages in the world including Bengali letters, Cyrillic letters, or Bopomofo symbols. Every Unicode character above hex 00C0 is legal in an identifier. Legal Java identifiers shows some example valid Java identifiers.

Table 7-1. Legal Java identifiers

calories

Häagen_Dazs

déconnage

_99

Puñetas

fottío

i

$__

p

The more accented characters you use in your variable names, the harder it is for others to edit them and maintain the code. So you should stick to the simple ASCII characters.

When a field or method can be forward-referenced

A forward reference is the use of a name before that name has been defined, as in the following:

class Fruit {
     void setWeight() { grams = 22; } // grams not yet declared

     int grams;
}

The example shown compiles with no problem. You can declare fields in any order, with one exception. The rule of thumb is that the compiler needs to see a field before it is used in the initialization of another field:

int i = grams;

The declaration of grams would have to appear before the int i declaration. The actual rule is somewhat more complicated, involving cases of nested classes (JLS section 8.3.2.3), but if you follow the rule of thumb you'll avoid the complexities. It is intended to avoid circular initializations.

When a class can be forward-referenced

A class can always be forward-referenced. The declaration of a class generally doesn't need to appear before the use of that class, as long as the compiler finds it at some point during compilation.

This is another benefit of “object variables are really pointers”. The compiler already knows how much memory to allocate for the object reference (one pointer's worth), and it knows how to reach members through the reference. It doesn't know how much space to allocate for the object itself, nor what the offsets of the members are, and it doesn't need to know this, because everything is accessed indirectly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.36