Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reflection and Non-Default Constructors

Reply
Thread Tools

Reflection and Non-Default Constructors

 
 
Lyndsey.Ferguson@gmail.com
Guest
Posts: n/a
 
      10-05-2005

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);
}
}
} ///:~

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      10-06-2005
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


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-07-2005
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.
 
Reply With Quote
 
Lyndsey.Ferguson@gmail.com
Guest
Posts: n/a
 
      10-14-2005

Hi Roedy,

You wrote that "if [am] a newbie, [I] 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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-14-2005
On 14 Oct 2005 11:43:17 -0700, ""
<> wrote or quoted :

>You wrote that "if [am] a newbie, [I] 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.
 
Reply With Quote
 
Lyndsey.Ferguson@gmail.com
Guest
Posts: n/a
 
      10-17-2005
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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-17-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57