Community
Participate
Working Groups
The UI marks the use of the "class literal" as an "invalid Expression" (literal message is "Syntax error on token "class", invalid Expression"). The class literal is a valid Java construct and should not be marked invalid. The root of this error is likely at a lower level within the JDT. The class literal is defined in the Java Language Specification as follows: --------- 15.8.2 Class Literals A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a `.' and the token class. The type of a class literal is Class. It evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance. --------- The following should be valid code: package foo; public class bar { static String myVar = class.getName() + " is my fully qualified classname"; public void printName() { System.out.println (myVar); } } The output from calling printName() on a bar instance should be: foo.bar is my fully qualified classname
...more specifically, the Eclipse UI underlines the word "class" in the example provided, and marks a red "X" in the column next to the line: public class bar { X static String myVar = class.getName() + " is my fully qualified classname"; ----- ... Mousing over the X gives the error message reported.
Please read closer the spec. Its syntax is a type name + dot + 'class', e.g. String.class