Reputation: 568
What is the difference between the Java class Class
(which extends Object
) and the class
keyword (the word that is used to create a class)?
Upvotes: 6
Views: 5277
Reputation: 1676
In programming, reflection is the ability of a program to modify its own structure and behavior at runtime through analysis of runtime details, such as the actual implementing class of an object instance. The Class
class is a part of the Java API for the purposes of reflection. Whereas the class
keyword is a structure of the Java language marking the definition of a new class, the Class
class is used to type variables and parameters as classes themselves. It's a way for the program to use class definitions themselves as objects to program around (you can programmatically enumerate over the class' public methods, for example).
Additionally, any instance o
of type Object
(that is, any object in Java at all) inherits the getClass
instance method. This yields the actual runtime class of the object, irrespective of the compile-time code-stated class of the variable the object is stored in. For example, for some defined class X
:
Object o = new X();
Class<?> type = o.getClass();
type
will now be a reference to the X
class itself, which matches the Class<?>
generic description because the X
class is in fact an instance of type Class<X>
.
Every defined class also has one static variable called class
referring back to the class itself. Calling getClass
on an instance of class X will return the same Class<X>
instance as that class' class
static variable:
("some string").getClass() == String.class
It is worth noting that the primitive types all have a static class
variable as well even though they are not Objects. It is also worth noting that a primitive type's class
is not the same as the class
of its wrapper class:
int.class != Integer.class
Upvotes: 6
Reputation: 559
Class
is a Java class just like Object
and String
are classes.
On the other hand, if we say:
Class<?> c = String.class;
What we get in return is a Class
object of type String
:
Class<String>
In short, both are essentially the same, but you can keep a reference of a String.class
object using the Class
class.
They're used for Java reflection which you can learn more about here: http://docs.oracle.com/javase/tutorial/reflect/
Upvotes: 2
Reputation: 6665
Class
objects are Objects
of special types.. of class java.lang.Class
used to represent class definiton.
class keyword is a keyword used to represent classes where you can put methods and variables.The most important thing to do with a Class Object is to perform Reflection API,it can be used to dynamically create objects,invoke methods and introspect them.You can also load multiple versions of a classes using java.lang.ClassLoader
Upvotes: 0
Reputation: 6005
Class
is a Java class type (that extends the Object class, as you said), "an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions, methods)." - Wikipedia
On the other hand, class
, is a keyword, i.e. a reserved word by the language compiler (or interpreter, in other cases) that can not be used as an identifier or any other use. This specific keyword is used to declare custom class types.
Upvotes: 0
Reputation: 2223
Class
is a "Java class" that provide methods to manipulate "Java class" while class
is a Java keyword used to declare a "Java class"
Upvotes: 1