Newbie - what is the class Class?

Z

zalek

I am looking on some code where I see the method getClass() is used.
From Java site I read that Class class is "Instances of the class
Class represent classes and interfaces in a running Java application.
As I understand instance of a class is an object - so what a point to
get a Class from an object?

Thanks,

Zalek
 
A

Arne Vajhøj

zalek said:
I am looking on some code where I see the method getClass() is used.
From Java site I read that Class class is "Instances of the class
Class represent classes and interfaces in a running Java application.
As I understand instance of a class is an object - so what a point to
get a Class from an object?

Lookup the methods of the class in the docs, then its use should
be obvious.

The keyword is "reflection".

Arne
 
J

Joshua Cranmer

zalek said:
I am looking on some code where I see the method getClass() is used.
From Java site I read that Class class is "Instances of the class
Class represent classes and interfaces in a running Java application.
As I understand instance of a class is an object - so what a point to
get a Class from an object?

The Class object is an object representing metadata about the class.
Most of the time, one doesn't need it. However, it is useful in two areas:
1. Generics reification hacks. It's the only way to do something like
"new T()" at this point.
2. A branch of programming called reflection. A simple description of
this is the ability to do dynamic operations, such as implementing a
scripting language in Java.

I do realize that the Generics reification hack is a special case of the
latter, but it is widely-used enough in its own sense (IMHO) to warrant
being listed separately. And this is by no means a complete list of
where one might want to use Class objects.
 
L

Lew

Jane, we were right. It *was* NewsMaestro!

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, degenerate, Zionism, fascism, genocide, Illuminati, Freemasons]

Listen to the Jewish banker, Paul Warburg:

"We will have a world government whether you like it or not.
The only question is whether that government will be achieved
by conquest or consent."

(February 17, 1950, as he testified before the US Senate).
 
I

Ian Shef

The Class object is an object representing metadata about the class.
Most of the time, one doesn't need it. However, it is useful in two areas:
1. Generics reification hacks. It's the only way to do something like
"new T()" at this point.
2. A branch of programming called reflection. A simple description of
this is the ability to do dynamic operations, such as implementing a
scripting language in Java.

The book _Java Reflection in Action_ by Ira R. Forman and Nate Forman is
useful for a deeper understanding of metadata and reflection.

There is even a figure that will help understand how a Class object can
extend from Object and yet describe any Java object (including a Class
object).

Published prior to JDK 1.5, it doesn't have much about Generics. There is a
little in the back, based on what was known about Generics at the time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top