From: zalek on
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

From: Arne Vajhøj on
zalek wrote:
> 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
From: Roedy Green on
On Fri, 4 Jul 2008 16:03:35 -0700 (PDT), zalek
<zalekbloom(a)hotmail.com> wrote, quoted or indirectly quoted someone
who said :

> - so what a point to
>get a Class from an object?

see http://mindprod.com/jgloss/classforname.html

that will get you started.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
From: Joshua Cranmer on
zalek wrote:
> 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.


--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
From: Lew on
Joshua Cranmer wrote:
> zalek wrote:
>> 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.

One very common use, probably the most common, of reflection is the
Class#newInstance() method. It's also the least complicated. It's useful for
idioms like maintaining a Map of labels to action handlers, for example.

Map <String, Class<? extends Handler>> handlers
= new HashMap <String, Class<? extends Handler>> ();

After one fills the Map, later some other logic can produce a String
representing some desired sub-module of logic, then use the Map to get an
instance of the corresponding handler.

(error-handling omitted, but don't do that in real life)

public void handle( String thing ) throws Exception
{
Class <? extends Handler> clazz = handlers.get( thing );
Handler handler = clazz.newInstance();
handler.handle();
}

One adds error handling, logging and thread synchronization to that as
appropriate.

--
Lew
 |  Next  |  Last
Pages: 1 2
Prev: Where to find libraries?
Next: JPanel in a JFrame???