This creates an object reference
log
Its type will be
Logger
The
Logger class has a
static method called
getLogger; actually, it has a number of methods called
getLogger, each taking different arguments. One of them takes a
Class object as the parameter. I think this is the point where you got confused.
In
Java, you normally use variables that are of primitive types (int, long, boolean...), or 'real' objects like
String, Date, List (or arrays, for that matter, such as int[]).
Each of those types, whether primitive or 'real' object, is represented by a type.
In real life, you may have people Agnes, Susana, John and Peter; they would be represented by objects in your code. The classes representing them could be 'Woman' and 'Man'. The class representing their name is String; the type representing their year of birth is int.
The class Class (ouch!) is the meta-information which tells you what type the object is.
You can try this:
So you can have variables/objects that represent the type of something.
classOfNow will be a reference to a Class object, actually a reference to
Date.class. Similarly,
classOfName will be a reference to a Class object,
String.class.
These objects can be used as method arguments, like in the call above.
This code says: get me a Logger object that is used to log messages coming from NameClass (it's just a convention).
You can even invoke methods on a Class object, such as newInstance(), which is equivalent to calling the default constructor, and will create a new object.
Or print the name of the class:
This is actually
Logger.getLogger(Some.class) does: it reads the simple class name, and creates/fetches a Logger object that is configured with that name. In the log configuration, you can then set up filters and formatters based on the class name, for example you can include the class name in each debug message so you know where it originated from.
There are even classes to represent primitive types, for example int.class, and classes to represent arrays of some type, such as int[].class or String[].class.
You'll notice that in Java 5 and above, Class can have a type parameter to assure type safety (refer to Java Generics and Collections for a detailed description, or at least read
http://java.sun.com/docs/books/tutorial/java/generics/index.html:
I hope I did not confuse you even more.
So
NameClass.class is an object that happens to represent a type, not a 'regular' value such as "John", but it's a proper object, you can pass it as arguments, return it as a result, invoke methods on it, assign it to variables.