Home > Articles

  • Print
  • + Share This

Working with Variables

Data types come into play when you need to declare variables to hold values that are important to your program. Variables can be either primitives or reference types. They are always declared within a class but a variable might be associated with the class as a whole or its scope might be limited to a method or a block of statements within a method.

Declaring a Variable

Here's a simple example of declaring an integer variable:

int maxLoginAttempts = 3;

When you create a variable in Java, you must know a few things:

  • You must know what data type you are going to use. In this case, that was the int primitive.

  • You must know what you want to call the variable (maxLoginAttempts).

  • You might also want to know the value with which the variable should be initialized. In this case, an initial value of 3 was assigned as part of the declaration. If you do not specify an initial value when you declare a variable as an attribute of a class, the Java compiler automatically assigns a default (0 in the case of int). Variables declared within a method are not assigned default values, so you must explicitly set them before you can use them.

You can create any variable in Java in the same way as was just shown:

  1. State the data type that you will be using (int).

  2. State the name the variable will be called (maxLoginAttempts).

  3. Assign the variable a value (=3).

  4. As with every other statement in Java, terminate the declaration with a semicolon (;).

Identifiers: The Naming of a Variable

Refer to the first example:

int maxLoginAttempts = 3;

How does Java use the characters that make up the string maxLoginAttempts?

maxLoginAttempts is called an identifier in programming terminology. Identifiers are important because they are used to represent a whole host of things. In fact, identifiers are any names chosen by the programmer to represent variables, constants, classes, objects, labels, or methods. After an identifier is created, it represents the same object in any other place it is used in the same code block.

There are several rules you must obey and points to consider when creating an identifier:

  • The first character of an identifier must be a letter, an underscore (_), or a currency symbol ($, £, and so on). Subsequent characters can be any of these or numerals.

Note - Although it is allowed, Sun's "Code Conventions for the Java Programming Language" advises against using an underscore or currency symbol to begin an identifier. This is a non-standard practice that results in identifiers that are difficult to read and that can easily be mistyped.

  • The characters do not need to be Latin letters or numerals; they can be from any alphabet. Because Java is based on the Unicode standard, identifiers can be in any language, such as Arabic-Indic, Devanagari, Bengali, Tamil, Thai, or many others.

  • In Java, as in C/C++ and many other modern languages, identifiers are case sensitive and language sensitive.

    This means that maxLoginAttempts is not the same as MaxLoginAttempts. Changing the case changes the identifier so that it cannot refer to the same variable. To aid the reader in distinguishing identifiers, it is standard practice in Java to name variables using identifiers that start with a lowercase letter and use mixed case to separate words.

Note - The language sensitivity of Java means that language differentiates identifiers in the same way case does. Identifiers are unique between languages even if the characters appear the same visually.

  • An identifier cannot be the same as one of the Java reserved words listed in Chapter 2, "HelloWorld: Your First Java Program."

  • Make your identifier names long enough to be descriptive. Most application developers walk the line between choosing identifiers that are short enough to be quickly and easily typed and those that are long enough to be descriptive and easily read. Remember that the time spent reading source code to maintain it is typically many times greater than that spent to develop it. Meaningful identifiers will always add to the clarity of your work.

Table 3.2 shows several examples of legal and illegal identifiers based on the preceding rules. Example causes of illegal identifiers include starting an identifier with a numeral, using a special character, or embedding whitespace. Notice the last example that contains a hyphen. Java would try to treat this as an expression containing two identifiers that are being subtracted.

Table 3.2 Examples of Legal and Illegal Identifiers

Legal Identifiers

Illegal Identifiers






Hot Java





  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.