Variables and Constants in Java

As in every programming language, variables are used to store values. Con­stants are variables whose values don’t change. In the following sections, you will learn how to declare variables and constants.

1. Declaring Variables

In Java, every variable has a type. You declare a variable by placing the type first, followed by the name of the variable. Here are some examples:

double salary;

int vacationDays;

long earthPopulation;

boolean done;

Notice the semicolon at the end of each declaration. The semicolon is neces­sary because a declaration is a complete Java statement, and all Java statements end in semicolons.

A variable name must begin with a letter and must be a sequence of letters or digits. Note that the terms “letter” and “digit” are much broader in Java than in most languages. A letter is defined as ‘A’-‘Z’, ‘a’-‘z’, ‘_’, ‘$’, or any Unicode character that denotes a letter in a language. For example, German users can use umlauts such as ‘a’ in variable names; Greek speakers could use a n. Similarly, digits are ‘0’-‘9’ and any Unicode characters that denote a digit in a language. Symbols like ‘ + ‘ or ‘©’ cannot be used inside variable names, nor can spaces. All characters in the name of a variable are significant and case is also significant. The length of a variable name is essentially unlimited.

You also cannot use a Java reserved word as a variable name.

As of Java 9, a single underscore _ cannot be used as a variable name. A future version of Java may use _ as a wildcard symbol.

You can declare multiple variables on a single line:

int i, j; // both are integers

However, we don’t recommend this style. If you declare each variable separately, your programs are easier to read.

2. Initializing Variables

After you declare a variable, you must explicitly initialize it by means of an assignment statement—you can never use the value of an uninitialized variable. For example, the Java compiler flags the following sequence of statements as an error:

int vacationDays;

System.out.println(vacationDays); // ERROR–variable not initialized

You assign to a previously declared variable by using the variable name on the left, an equal sign (=), and then some Java expression with an appropriate value on the right.

int vacationDays;

vacationDays = 12;

You can both declare and initialize a variable on the same line. For example:

int vacationDays = 12;

Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:

double salary = 65000.0;


int vacationDays = 12; // OK to declare a variable here

In Java, it is considered good style to declare variables as closely as possible to the point where they are first used.

3. Constants

In Java, you use the keyword final to denote a constant. For example:

public class Constants


public static void main(String[] args)


final double CM_PER_INCH = 2.54;

double paperWidth = 8.5;

double paperHeight = 11;

System.out.println(“Paper size in centimeters: “

+ paperWidth * CM_PER_INCH + ” by ” + paperHeight * CM_PER_INCH);



The keyword final indicates that you can assign to the variable once, and then its value is set once and for all. It is customary to name constants in all uppercase.

It is probably more common in Java to create a constant so it’s available to multiple methods inside a single class. These are usually called class constants. Set up a class constant with the keywords static final. Here is an example of using a class constant:

public class Constants2


public static final double CM_PER_INCH = 2.54;

public static void main(String[] args)


double paperWidth = 8.5; double paperHeight = 11;

System.out.println(“Paper size in centimeters: “

+ paperWidth * CM_PER_INCH + ” by ” + paperHeight * CM_PER_INCH);



Note that the definition of the class constant appears outside the main method. Thus, the constant can also be used in other methods of the same class. Furthermore, if the constant is declared, as in our example, public, methods of other classes can also use it—in our example, as Constants2.CM_PER_INCH.

4. Enumerated Types

Sometimes, a variable should only hold a restricted set of values. For example, you may sell clothes or pizza in four sizes: small, medium, large, and extra large. Of course, you could encode these sizes as integers 1, 2, 3, 4 or characters S, M, L, and X. But that is an error-prone setup. It is too easy for a variable to hold a wrong value (such as 0 or m).

You can define your own enumerated type whenever such a situation arises. An enumerated type has a finite number of named values. For example,


Now you can declare variables of this type:

Size s = Size.MEDIUM;

A variable of type Size can hold only one of the values listed in the type dec­laration, or the special value null that indicates that the variable is not set to any value at all.

We discuss enumerated types in greater detail in Chapter 5.

Source: Horstmann Cay S. (2019), Core Java. Volume I – Fundamentals, Pearson; 11th edition.

Leave a Reply

Your email address will not be published. Required fields are marked *