Reflection and Non-Default Constructors

  • Thread starter Lyndsey.Ferguson
  • Start date
L

Lyndsey.Ferguson

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

Chris Uppal

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
 
R

Roedy Green

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

Lyndsey.Ferguson

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
 
R

Roedy Green

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

Lyndsey.Ferguson

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
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top