Newbie - what is the class Class?

Discussion in 'Java' started by zalek, Jul 5, 2008.

  1. zalek

    zalek Guest

    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
     
    zalek, Jul 5, 2008
    #1
    1. Advertising

  2. zalek

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Jul 5, 2008
    #2
    1. Advertising

  3. zalek

    Roedy Green Guest

    Roedy Green, Jul 5, 2008
    #3
  4. 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
     
    Joshua Cranmer, Jul 5, 2008
    #4
  5. zalek

    Lew Guest

    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).
     
    Lew, Jul 6, 2008
    #5
  6. zalek

    Ian Shef Guest

    Joshua Cranmer <> wrote in news:wQybk.260$wa1.103
    @trndny07:

    > 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.


    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.
     
    Ian Shef, Jul 11, 2008
    #6
  7. zalek

    Roedy Green Guest

    On Fri, 4 Jul 2008 16:03:35 -0700 (PDT), zalek
    <> 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
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 12, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,958
    Thomas Weidenfeller
    Oct 12, 2005
  2. Snoeys Andy
    Replies:
    2
    Views:
    429
    White Wolf
    Sep 12, 2003
  3. christopher diggins
    Replies:
    16
    Views:
    792
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    631
  5. SzH
    Replies:
    1
    Views:
    363
Loading...

Share This Page