Reflection and Non-Default Constructors

Discussion in 'Java' started by Lyndsey.Ferguson@gmail.com, Oct 5, 2005.

  1. Guest

    Hello Everyone,

    I'm learning Java (using Bruce Eckel's Thinking In Java 3rd Ed) and I'm
    in chapter 10 on the Exercises. One of the questions is to use
    reflection to create a "Toy" object using the non-default constructor.

    Can you comment on how I've done it in the code provided below? I would
    like to know if there is a better way. Perhaps to have the program get
    the list of constructors and then figure out how to get the correct
    parameter list. Comments/Suggestions?

    Thanks in advance,
    Lyndsey


    class Toy {
    Toy() {}
    Toy(int i) { System.out.println("Creating Toy(" + i + ").");}
    }

    public class ToyTest {

    public static void main(String[] args) {
    Class c = null;
    try {
    c = Class.forName("c10.Toy");
    } catch(ClassNotFoundException e) {
    System.out.println("Can't find c10.Toy");
    System.exit(1);
    }
    Object o = null;
    Object o2 = null;
    try {
    // Requires default constructor:
    o = c.newInstance(); // (*1*)

    // Create an object using the non-default constructor
    Class[] typeList = { int.class, };
    Constructor constructor = cy.getDeclaredConstructor(typeList);
    Object[] argList = { new Integer(999), };
    o2 = constructor.newInstance(argList);

    } catch(Exception e) {
    System.out.println("Exception that we are not going to look at
    for now.");
    System.exit(1);
    }
    }
    } ///:~
     
    , Oct 5, 2005
    #1
    1. Advertising

  2. Chris Uppal Guest

    wrote:

    > Can you comment on how I've done it in the code provided below? I would
    > like to know if there is a better way. Perhaps to have the program get
    > the list of constructors and then figure out how to get the correct
    > parameter list. Comments/Suggestions?


    Your code seems perfectly reasonable to me. That's the way I'd have coded it
    myself ;-)

    Just two minor observations:

    You have chosen to use getDeclaredConstructor() rather than getConstructor(),
    the difference is that getConstructor() only looks for public constructors.
    For some purposes that would be the better option, though it doesn't really
    make any difference for exercises like this (especially as the two Toy
    constructors are not public !).

    (If this paragraph confuses you then please just ignore it) Since JDK 1.5 you
    don't need to create the parameter arrays explicitly. You can just say:
    Constructor constructor = c.getDeclaredConstructor(int.class);
    o2 = constructor.newInstance(999);
    It's important to realise that all that's happening is that under the hood the
    compiler is generating exactly the same code as you originally wrote, it's just
    that those two methods have been made "varadic" -- which means that they take a
    variable list of arguments which the compiler packs into an array for you.

    -- chris
     
    Chris Uppal, Oct 6, 2005
    #2
    1. Advertising

  3. Roedy Green Guest

    On 5 Oct 2005 07:08:19 -0700, ""
    <> wrote or quoted :

    >Can you comment on how I've done it in the code provided below?

    If your code works, I can't imagine how it could be done in a much
    shorter way in 1.4. With 1.5 you can often avoid code of the form;

    new xxx[] { ..... }

    with a ... parm.

    If you are a newbie, you have a great career ahead of you.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 7, 2005
    #3
  4. Guest

    Hi Roedy,

    You wrote that "if [am] a newbie, have a great career ahead of
    [me]." Why do you say that? Just curious.

    Thanks for the comments, btw. Also thanks to you Chris.

    Lyndsey
     
    , Oct 14, 2005
    #4
  5. Roedy Green Guest

    On 14 Oct 2005 11:43:17 -0700, ""
    <> wrote or quoted :

    >You wrote that "if [am] a newbie, have a great career ahead of
    >[me]." Why do you say that? Just curious.


    The code was exceptional quality for a newbie. The problem was an
    exceptionally difficult one for a newbie. It showed unusual
    curiosity. So if you can do that well just starting out, just think
    what you will be able to do with some experience under your belt.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 15, 2005
    #5
  6. Guest

    Ah, okay :)

    Thank you for the compliment.

    Actually, I do have a fair amount of experience under my belt with C++
    and its STL as well as Software Engineering's Best Practices, Design
    Patterns, etc. So I cannot claim to be a exceptional newbie :)

    I thought perhaps you were alluding to the amount of opportunities
    available to Software Engineers who develop primarly in Java.

    Cheers!
    Lyndsey
     
    , Oct 17, 2005
    #6
  7. Roedy Green Guest

    On 17 Oct 2005 11:41:06 -0700, ""
    <> wrote or quoted :

    >I thought perhaps you were alluding to the amount of opportunities
    >available to Software Engineers who develop primarly in Java.


    that is not as rosy as it was a few years ago. Clever programmers in
    eastern Europe and India are willing to work for little money and now
    a days you can work on a Java team without ever meeting your boss face
    to face.

    With email you will likely have more communication than if you lived
    in the next cubicle.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 17, 2005
    #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. Dave Rudolf
    Replies:
    12
    Views:
    8,303
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    589
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    608
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    399
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    470
Loading...

Share This Page