Naming things in Java (identifiers)
The name given to a variable, operation,
attribute, or class is called its identifier. Java has certain rules about identifiers,
which must be followed. There are also certain conventions, which ought to
be followed.
Identifier really means the same thing as
name, but identifier is the term normally used by programmers. All variables and classes
must have an identifier. The most important of Java's rules about identifiers are that:
- identifiers cannot be the same as a keyword
- identifiers are case-sensitive
- identifiers cannot contain spaces or most punctuation
characters
We cannot have a variable called 'class',
since class is a keyword. However, we can have one
called 'Class', since Java is case sensitive so 'class' and 'Class' are considered
different.
Warning: You are likely to make many simple
mistakes of using upper and lower case first letters the wrong way round. Don't worry
about this, just check the case of your identifiers when you encounter an error you don't
understand.
Conventions have evolved to reduce the chance
of errors, and to use case to help us distinguish Class identifiers from variable and
method identifiers. The most important conventions are that
- names of classes start with a capital letter
- names of everything else start with a lower-case letter
- if name is a phrase rather than a word, then capital letters
are used to separate the words
For example, we cannot define a variable
called number of seconds because spaces are not allowed. We can define a variable
called number_of_seconds (since one of the few
punctuation symbols permitted in identifiers is the underscore). However, in Java
programming the convention is to write such a composite identifier as numberOfSeconds.
Of course you don't have to follow the
conventions; the compiler won't object if you don't. However, there are lot of Java
programmers in the World, and they are nearly all following these conventions. It will
make your programs easier to understand if you do too. Also, if you use the same
conventions as everyone else, you'll find their programs easier to understand!
What about confusion between variable and
method identifiers?
You may be wondering how we can distinguish
between variable and method identifiers, if they both start with a lower case letter. In
fact this is not a problem, since a method must always be followed with parentheses
"(" ")" even if it has no arguments.
Thus we know, for example, that length is a
variable identifier, and length() is a method identifier.
Back
to top
. 
RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|