Results 1 to 5 of 5
  1. #1
    kroiz is offline Member
    Join Date
    Jul 2009
    Posts
    12
    Rep Power
    0

    Question classes as objects

    hi
    I want learn about this thing I see in the following example
    What I don't understand is the use of the classes as though they were objects to pass to the array.

    Java Code:
    //All we do here is assemble the list of WizardPage subclasses we
    //want to show:
    Class[] pages = new Class[] {
        AnimalTypePage.class,
        LocomotionPage.class,
        OtherAttributesPage.class,
        FinalPage.class
    };
            
    //Use the utility method to compose a Wizard
    Wizard wizard = WizardPage.createWizard(pages, WizardResultProducer.NO_OP);
    I can not even name it so could not find info about it in google.
    Thank you.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,940
    Rep Power
    19

    Default

    Quote Originally Posted by kroiz View Post
    hi
    I want learn about this thing I see in the following example
    What I don't understand is the use of the classes as though they were objects to pass to the array.

    Java Code:
    //All we do here is assemble the list of WizardPage subclasses we
    //want to show:
    Class[] pages = new Class[] {
        AnimalTypePage.class,
        LocomotionPage.class,
        OtherAttributesPage.class,
        FinalPage.class
    };
            
    //Use the utility method to compose a Wizard
    Wizard wizard = WizardPage.createWizard(pages, WizardResultProducer.NO_OP);
    I can not even name it so could not find info about it in google.
    Thank you.
    Consider it a shorthand for Class.forName() method. It's a way of easily getting the Class object representing that class name.

  3. #3
    kroiz is offline Member
    Join Date
    Jul 2009
    Posts
    12
    Rep Power
    0

    Default

    Does it creates an object of the class, like an instance of it?
    In the example above, the function createWizard gets an array of such classes, can you help me understand how is that could be useful instead of plain instances?
    does it has anything to do with reflection?
    Last edited by kroiz; 07-24-2009 at 09:24 PM.

  4. #4
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    It has everything to do with reflection :) when getting an instance of a Class object you are NOT getting an instance of that particular class. For instance:
    Java Code:
       Class stringClass = String.class;
       String someString = "someString";
    The difference is the object stringClass is the actual representation of the String class whereas someString IS an implementation of the String class. What this means is the object stringClass will allow you to do things like find out what Fields the String class has for instance:
    Java Code:
       Field[] fields = stringClass.getDeclaredFields();
    This allows you the capability to work with classes you know nothing about at runtime. Reflection is pretty powerful and fun once you learn it's uses :) Hopefully this example did not confuse you more.

  5. #5
    kroiz is offline Member
    Join Date
    Jul 2009
    Posts
    12
    Rep Power
    0

    Default

    ohhhhhh aaaaahhhh
    Now I get it!
    I was once asked about it in a job interview. :confused: I did not know what it was, so I got home and read about it and thought: what a useless piece of mechanism - I spent years programming in C++ without earing of it.
    I was just disgruntled because I did not know. :D
    I understand its usefulness now.

    In the Wizard API project, I guess it is used for being able to observe the wizard's pages for changes in their fields.

    Thank you all, very very much for taking the time to help me.
    Have a great day.

Similar Threads

  1. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 02:41 PM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. Replies: 1
    Last Post: 11-18-2008, 04:48 PM
  4. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •