Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2008

Tip: Looking for answers? Try searching our database.

Newbie -  what is the class Class?

Thread view: 
zalek - 05 Jul 2008 00:03 GMT
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
Arne Vajhøj - 05 Jul 2008 00:12 GMT
> 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
Roedy Green - 05 Jul 2008 00:55 GMT
On Fri, 4 Jul 2008 16:03:35 -0700 (PDT), zalek
<zalekbloom@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.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Joshua Cranmer - 05 Jul 2008 01:00 GMT
> 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.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Lew - 05 Jul 2008 02:47 GMT
>> 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
[quoted text clipped - 14 lines]
> 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.

Signature

Lew

Ian Shef - 11 Jul 2008 20:41 GMT
Joshua Cranmer <Pidgeot18@verizon.invalid> wrote in news:wQybk.260$wa1.103
@trndny07:

>> 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
[quoted text clipped - 9 lines]
> 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.
Roedy Green - 12 Jul 2008 11:18 GMT
On Fri, 4 Jul 2008 16:03:35 -0700 (PDT), zalek
<zalekbloom@hotmail.com> wrote, quoted or indirectly quoted someone
who 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?

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


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.