43

In my strings.xml file I have

<string name="continue">Continue</string>

I can't build my project because of the error: "Invalid symbol: 'continue'". Why I can't use such a name?

Eugene
  • 59,186
  • 91
  • 226
  • 333

2 Answers2

63

It's because continue is a reserved symbol in Java, so you cannot use it as a name for any object in your XML files or Java code.

The reason this is a problem is that the XML defined in your project is translated into Java code that the Dalvik VM can understand. So, your code above translates into the following in R.java:

public final class R {
    public static final class string {
        public static final int continue=0x7f040000;
    }
}

The problem is more obvious when examining the (would-be) generated code.

See list of reserved Java symbols for others to avoid.

Justin ᚅᚔᚈᚄᚒᚔ
  • 15,081
  • 7
  • 52
  • 64
  • thanks so much for the hint. I've been trying to use a continue button in my app with the name continue, and now I know why my R.java could not be generated. – Honey H Jan 03 '13 at 02:19
  • So is there any work around? I am facing same problem but in my case string is "void" i can't avoid writing it. – Gem Jul 16 '15 at 18:54
  • The string *contents* can be a keyword, but the *'name' attribute* cannot be a keyword. So, `Void` won't work, but `Void` is fine. – Justin ᚅᚔᚈᚄᚒᚔ Jul 17 '15 at 19:27
6

"continue" is a Java keyword and the R.java would not compile.

public static final int continue=0x7f040001;

the above code would cause an "Syntax error on token "continue", invalid VariableDeclaratorId" error.

lukuluku
  • 4,344
  • 3
  • 30
  • 35