Re: help: a reflection question
On 18-02-2011 14:01, Lew wrote:
www wrote:
//BUT, MyTest.java has import statements at the top:
import ProgramA;
import ProgramB;
import ProgramC;
If your program needs knowledge of those classes, as you have coded it
to do, then you have asked for the dependence on those classes.
'import' statements from the default package don't make sense. Why
would you do that?
The problem is that if I want to wrap MyTest.class into a JAR file to
give to somebody who is interested in using MyTest.class to test his
ProgramW.java which has nothing to do with ProgramA, B or C, I also
need to include ProgramA.class, ProgramB.class and ProgramC.class into
the JAR file. Plus, the code above won't work with ProgramW.java.
Of course. You have built those dependencies into your code.
He did state that himself.
He also explained what he wanted to do.
And needed help to get it working.
I understand polymorphism to have an interface Program.java for all
the programs. But I am wondering if reflection can achieve it without
a sharing interface.
What "it" do you mean? Do you mean to avoid dependencies? That is
better done with good design.
You *could* use reflection, but you *shouldn't*. Reflection is an
advanced topic suitable only for those who have learned to use
packages and interfaces. Master those first. Avoid reflection at all
costs for now.
Reflection is no cure for ignorance.
No.
But it is an alternative to casting to an interface.
And in this case casting to an interface has a serious
problem - main is static!
Arne