Results 1 to 6 of 6
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default List of keywords accessible to programs?

    Is there a way for a program to get a list of Java keywords from the standard API? Maybe via reflection or something? Right now my program contains this:

    Java Code:
    // javaKeywords is a HashSet<String>
    javaKeywords.add("abstract");
    javaKeywords.add("assert");
    javaKeywords.add("boolean");
    // etc...
    Just curious if I wasted my time typing all that.

    (I'm generating a JFlex lexer spec from another format, and I need to check that none of the identifiers are Java keywords.)
    Get in the habit of using standard Java naming conventions!

  2. #2
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

  3. #3
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Personally, I'm prone to code it like this:
    Java Code:
    	private static final String[] JAVA_LANGUAGE_KEYWORDS = {
    		"abstract",    "continue",   "for",        "new",        "switch",
    		"assert",      "default",    "goto",       "package",    "synchronized",
    		"boolean",     "do",         "if",         "private",    "this",
    		"break",       "double",     "implements", "protected",  "throw",
    		"byte",        "else",       "import",     "public",     "throws",
    		"case",        "enum",       "instanceof", "return",     "transient",
    		"catch",       "extends",    "int",        "short",      "try",
    		"char",        "final",      "interface",  "static",     "void",
    		"class",       "finally",    "long",       "strictfp",   "volatile",
    		"const",       "float",      "native",     "super",      "while",
    	};
    But that's just me. Do what you want.

  4. #4
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Maybe...
    Java Code:
    	private static final Set<String> JAVA_LANGUAGE_KEYWORDS = new HashSet<String>(java.util.Arrays.asList(new String[] {
    		"abstract",    "continue",   "for",        "new",        "switch",
    		"assert",      "default",    "goto",       "package",    "synchronized",
    		"boolean",     "do",         "if",         "private",    "this",
    		"break",       "double",     "implements", "protected",  "throw",
    		"byte",        "else",       "import",     "public",     "throws",
    		"case",        "enum",       "instanceof", "return",     "transient",
    		"catch",       "extends",    "int",        "short",      "try",
    		"char",        "final",      "interface",  "static",     "void",
    		"class",       "finally",    "long",       "strictfp",   "volatile",
    		"const",       "float",      "native",     "super",      "while",
    	}));

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    That's a handy version. A set is nice when you also want to avoid the literals "true", "false", and "null", since you can easily clone it and add those strings before testing whether it contains the strings in question.
    Get in the habit of using standard Java naming conventions!

  6. #6
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Yea; probably should make the Set immutable. Maybe add "true", "false", and "null" to the list of values. But those three aren't technically Java Language Keywords. But if they're Java Reserved Words, from the perspective of your program, well... Do what's most convenient for you.

Similar Threads

  1. final VS static keywords
    By hisheeraz in forum New To Java
    Replies: 7
    Last Post: 04-25-2011, 06:17 AM
  2. Some keywords
    By guisome in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 01:21 AM
  3. Global variable not accessible
    By bolduc4 in forum New To Java
    Replies: 3
    Last Post: 05-10-2010, 08:51 AM
  4. Lucene - Keywords and Strings
    By lucenenew in forum Lucene
    Replies: 0
    Last Post: 11-08-2009, 07:22 PM
  5. Reserved Keywords
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 11-30-2007, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •