VisionSet said:
All variables are either primitives or a reference to an Object.
If they are a reference to an Object as in the case above then they can have
a type of 1 or many.
I'm not sure what you mean by that, but perhaps it's that the class of
an object can be compatible with any positive number of types. It is
necessary here to be clear about the difference between class and type.
Every object has exactly one class. Every variable and expression has
exactly one type. A non-primitive types is expressed in terms of class
names, and it serves the purpose of describing to the compiler the
possible classes of the object that its value (a reference) refers to.
In the example above doHelloWorld is a reference to a concrete Object with
an interface Type of Runnable.
More precisely, doHelloWorld is a variable of type Runnable, which as
the result of the assignment holds a reference to an object whose class
implements Runnable and has Object as its direct superclass.
It has been created using an Annoymous class, since there is no class
declaration.
Quibbling a bit here, but there *is* a class declaration -- an anonymous
one. The syntax is a bit different from that for a named class
declaration, but that doesn't make it any less a declaration.
If the class was not annoymous then the Object would also have
the Type of that class.
Sorry, but that's nonsense. In the first place, Object is a superclass
of the anonymous class, therefore references to its instances are
assignment-compatible with type Object. *Every* class has Object as a
superclass, and therefore *every* reference is assignment-compatible
with type Object. In the second place, classes do not have types. They
have superclasses, and they have interfaces implemented; these determine
which types their instances are compatible with. They do not have types.
Runnable // the Type you are assigning Runnable // the type of ...
doHelloWorld // the variable you are assigning it to
doHelloWorld // the variable you are assigning a value to
= // assignment
new // instantiation
Runnable() {...} // annoymous class definition
Which one could also call an anonymous class declaration.
[...]
this bit :
Runnable() {...} // annoymous class definition
you can think of expanding to:
private class AnnoymousClass implements Runnable {
...
}
Yes, that's quite right. More right, in fact, than the idea that the
class in question really has no name. The Java source code assigns no
name to it, but the Java compiler invents one at compile time. The VM
doesn't have any concept of a class that doesn't have a name; "anonymous
class" really means something more like "class whose name I don't know
and don't care about."