Dim
Dim

Reputation: 4807

Use enum with number

I know there are many alternatives to reach what I wish, but I wont this solution because it is the most comfortable to me. I wish to use enum that starts with number, like so.

public enum Quality {
    1080p,
    720p,
    BlueRay //this one OK
}

And then use it like so when converting to string:

Quality.1080p.name();

Why it is not possible?

Upvotes: 0

Views: 36

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93559

Because the Java language doesn't allow variable names to start with a number- just a letter or underscore. Any character after the first may be a number. The main reason for this is to make parsing easier, and prevent situations where the parser can't tell if a symbol is a number or a variable name.

For example, if numbers were valid at the start of a variable I could do the following:

String 1 = "string";
System.out.println(1);

Does this print 1 or "string"? They avoid the problem by not allowing it. Many (most?) languages have that restriction.

Upvotes: 1

Related Questions