I’m building a small Android application, but this is more of a Java question than an android question. Looking through the tutorials there are lines that look like:
startService(new Intent(this, MyService.class));
what exactly does the “MyService.class” field represent? Is that just a reference to the class for a template?
Thanks.
Andy’s answer is definitely correct, but I want to expand on the point a little.
.classis a special syntax for obtaining an instance of a Class object. It can be used when only the type is available and no instance of the related object is around. It can be used with any concrete type name, including arrays and primitives. For instance,int.classis valid.This way (and other ways) to get a Class object are documented in the old Sun reflection API docs.
The special
.classsyntax often appears in idiomatic usage as a “type token” in generic code. A class literal obtained with.classis called a type token when “passed among methods to communicate both compile-time and runtime type information” (Joshua Bloch’s Effective Java 2e, p. 142).