-2

Is there any difference in declaring static strings versus Enums. example:

public static final String SUNDAY = "SUNDAY";

or

enum WEEK
{
    SUNDAY;
}
Ikshvak
  • 205
  • 2
  • 8
  • 17
  • 3
    Yes. One is a `String`, the other is an `enum`. – Keppil Jul 23 '13 at 21:00
  • http://stackoverflow.com/a/1858927/2610743 - That will give you a lot of information about when to use typical constants and when to use Enums. – TravJenkins Jul 23 '13 at 21:01
  • This is highly subjective. Theorical differences? Many. Practical differences? pretty few in the context you intend to use them (you can even switch strings in Java 1.7+). – Fritz Jul 23 '13 at 21:07

2 Answers2

2

If your app deals with days of the week as a type, then using an enum maintains type safety, preventing you from making mistakes when handling days of the week. If you make it a string, your date class could just as easily print "Today is JOE SCHMOE, July 23rd."

John Deters
  • 4,295
  • 25
  • 41
1

Using an enum scopes the name. You have to write WEEK.SUNDAY instead of just SUNDAY, which will make your code cleaner. Also before 1.7, you could not use strings in switch statements.

sevensevens
  • 1,703
  • 16
  • 27