Hopefully, this doesn't muddy the waters even more.
There are three terms that need to be teased apart here:
1.
Variable. I like this definition:
http://www.cs.utah.edu/~germain/PPS/Topics/variables.html
It says that a variable is a
symbolic name for (or reference to) information. Examples:
Here,
x and
str are symbolic names. They represent some information that you will use in your program. They are also variables
because the values that they represent can change. EDIT: striking that out because it's a bit ambiguous and open to different interpretations. I think it's better to say that they are variables because they can be assigned different (or various/varying) values to represent during the execution of the program. That is, at some point, x might represent the value
7, then at another point in time, it will represent the value
13, and at yet another point, represent the value
-26. So, its name,
x, is the same but the value it represents can be different depending on what's happening in the program.
2.
Literal. I like this definition:
http://www.webopedia.com/TERM/L/literal.html
It says that a literal is "a value written exactly as it's meant to be interpreted." Examples:
Here,
5 and
"Hello" are literals.
5 is a numeric literal.
"Hello" is a String literal. In Java, String literals need to be surrounded by double quotes and all characters in between the quotes are part of the literal value. The quotes are not part of the literal value but they are required so that the Java compiler can tell them apart from symbolic names.
Here, the name of the class declared starting on line 1 is
Hello. On line 2, the instance variable
message is assigned a value of
"Hello" which, again, is a String literal. Note that because it is surrounded by double quotes, Java interprets the consecutive characters 'H', 'e', 'l', 'l', 'o' as being part of a String literal. On line 1, the same series of characters are interpreted as the name of a class. On line 4,
newMessage, is a symbolic name. It is also referred to as the "formal parameter name" or simply, "the
newMessage parameter". On line 5, the instance variable
message (declared on line 2) is assigned whatever value the
newMessage parameter refers to/represents. The only literal in this code is
"Hello" on line 2.
3.
Constant. It's confusing to say "constant variable" because those two words are opposites so I'll just stick with "constant". A constant is something that doesn't change its value. By
convention, constants in Java are given symbolic names that are in all capitals. Underscores in constant names are used to separate words. However, a name that follows this convention is not necessarily a constant. The convention, when followed, is only for consistency so that it's easy to tell variables and constants apart by sight. Java neither enforces nor recognizes this convention although most modern Java IDEs can and will warn you when you are NOT following it properly. In other words, just because a name follows the convention used for constants, it doesn't necessarily make it one.
Here,
STILL_A_VARIABLE is a symbolic name that follows the convention for naming constants in Java. However, it is
NOT a constant because its value can change. So,
STILL_A_VARIABLE is just, well, still a variable. To make it a constant, you'd have to qualify the declaration with the
final keyword.
Here, the symbolic name
LUCKY_NUMBER represents a constant value because it's declared as
final. This means that once it is assigned a value, which in this case is
7 (a literal right?), no other code can attempt to assign a new value to it. Any code that attempts to do so would result in a compile-time error. Note that because "
it's a symbolic name that represents a constant value" is a mouthful, we normally use a more pithy form and simply say that "LUCKY_NUMBER is a constant" to mean the same thing.
Constants are usually declared with the
public and
static keywords as well. That is, this is how most constants are declared:
The
public qualifier makes the symbolic name
LUCKY_NUMBER globally visible. The
static qualifier makes
LUCKY_NUMBER a class member. Without
static, it would be an
instance member, which doesn't make much sense semantically.
So, if you had this:
then, the proper way to reference the constant declared on line 2 is
LuckyNumber.SEVEN. In this code,
LuckyNumber is the class name,
SEVEN is a symbolic name and a constant, and
7 is the literal value assigned to it. Specifically,
SEVEN is a symbolic name of a constant that is a public class member of
LuckyNumber. The value of the
LuckyNumber.SEVEN constant was initialized on line 2 by assigning it the literal value
7.
On line 4,
pick is a symbolic name (it stands for something) for a variable (because its value can change over time). Specifically,
pick is an
instance variable because each instance of the class
LuckyNumber will have its own value of the variable
pick. The value of the
pick instance variable is initialized to whatever value the symbolic name
SEVEN represents (
7 right?). Because it is a variable, however,
pick may be assigned different values to represent later on. In this code, only the value
7 is a literal.
Let's assume that the following code is in the same package as the
LuckyNumber class:
On line 3,
myNum is a symbolic name. It is also a
variable because it can be changed. On line 4,
first is a variable (and a symbolic name). It is assigned the global constant value that the symbolic name
LuckyNumber.SEVEN represents. Because that's a mouthful to say and write, we simply say that "
first is assigned the constant
LuckyNumber.SEVEN." Again,
SEVEN here is a symbolic name that represents a global constant value and it evaluates to the integer value of
7. That is also the value that gets assigned to the variable
first.
SEVEN is NOT a literal value though, right?
On line 5, the
pick variable of the LuckyNumber instance referenced by the variable
myNum is assigned the literal value of
2.
On line 6, another LuckyNumber instance is created and a reference to it assigned to the variable named
strange.
On line 7, the
pick variable of the LuckyNumber instance referenced by the variable
strange is assigned a value that's calculated by adding the value of the constant
LuckyNumber.SEVEN and the literal value
6. The resulting value,
13, is what
strange.pick represents at that point. The value of the
myNum.pick instance variable would still be
2.
One last thing, Line 9 is commented out because it is illegal and will not compile.
SEVEN is declared as
final in the
LuckyNumber class so it cannot be assigned another value after it is initialized. Java will recognize this and throw a compile-time error if you uncomment line 9. If you remove the
final keyword from the declaration of
SEVEN, line 9 can be uncommented and it will compile.
In this code (ignoring line 9), only the values
2 and
6 are literals.
Clear as mud now?
Ok, one final note, as I can anticipate that the term "symbolic name" may add to your confusion. This kind of goes back to what Stephan wrote:
The word "tree" is a word, not a tree.
I suppose you could just say "name" instead of "symbolic name" but it's really just a technical term that computer scientists like to use because it reflects the significance of the name in the program. That is, these names represent or "symbolize" something in your program, so that when you use the name, it is understood that you are really referring to the thing that it stands for or symbolizes, just as your user name symbolizes your online identity, or how a flag symbolizes your country, or how a particular combination of shapes and lines can symbolize
a brilliant but eccentric musician.
I hope this helps.