Why are classes not defined symbols in Java, except when you access properties on them?

Curtis Parfitt-Ford :

Consider the simplest possible example. We have

public class Foo {}

Therefore we can say

Foo.class // ==> class Foo

It therefore stands to reason that Foo should be a java.lang.Class, as we clearly have access to it and can manipulate it in the current context. Yet

Foo instanceof java.lang.Class

raises the error

cannot find symbol
  symbol:    variable Foo
Foo instanceof java.lang.Class
^-^

This isn't anything to do with instanceof itself, though; just

Foo

alone in the OpenJDK shell produces the same error.

How can we be accessing properties on Foo if Foo does not exist in the current context?

rgettman :

A class name may be used in your source code as a part of many different expressions. It doesn't mean that the class name itself is an object; it just means that the name itself can be used as part of a bigger expression. These are not "properties" or attributes; they are other syntactical constructions used to form expressions with many different parts.

The JLS defines how a "TypeName", e.g. Foo can be used to form expressions in Chapter 15. Specifically:

It may also be a "Reference Type" eligible for other expressions, such as:

There are specific uses of a type name e.g. Foo that are described above. This does not imply that the type name can be used generally as its own object. Type names are allowed only as part of larger expressions that can be otherwise unrelated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322460&siteId=1