SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I like any of Rob's solutions better.
Reflection is slower and less clear to read. I only use it when "regular" code doesn't work. After all you are likely to visit code more often to read it that the first time you write it.
That said, I use reflection regularly. Mainly on test code where I want to do some "static analysis" on things.
sumanth kadaba wrote:
Hi Jeanne,
Well I am not sure what you meant by saying reflection is slow.. (invoking a method takes longer time? it must be similar to calling a function right?).
Steve
make sure you are allowed to call the method, in addition to the class hierarchy to search to find the correct method with correct parameters, and that the parameters of of the proper assignable type
Rob Prime wrote:
Deepak Lal wrote:
Thanks for the replies,but can you please tell me which solution should i go for....Please need your advice....
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
salvin francis wrote:what exactly is "add" in :
a variable ? or a class ? or something else ?
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu wrote:
salvin francis wrote:what exactly is "add" in :
a variable ? or a class ? or something else ?
Nothing but a runtime value which determines the actual method to be invoked! In the enum's arena, it is a enum variable/member.
salvin francis wrote:
DUDE !!
that enum thing is Amazing !!
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
salvin francis wrote:I do think the "else-if ladder" [thats what we call it here]
is the most optimal thing, but i may be wrong here.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
salvin francis wrote:a variable ? or a class ? or something else ?
Raghavan Muthu wrote:
salvin francis wrote:what exactly is "add" in :
a variable ? or a class ? or something else ?
Nothing but a runtime value which determines the actual method to be invoked! In the enum's arena, it is a enum variable/member.
A better solution is to make an abstraction, with an interface:
view plaincopy to clipboardprint?
1. interface Operation
2. {
3. public void execute();
4. }
5.
6. class Add implements Operation
7. {
8. public void execute() { add(); }
9. }
10. // etc
interface Operation { public void execute(); } class Add implements Operation { public void execute() { add(); } } // etcYou then need a mapping from condition to operation. You can use a Map<String,Operation> for that:
1. Operation operation = operations.get(condition);
2. operation.execute();
R van Vliet wrote:I think part of the confusion arises from the enum values not being uppercase, which i think is a relatively common convention, so i'd prefer to write it as :
That said, i find it an elegant solution that I never really think of when running into similar issues ;)
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions