- Import and use a type from org.gnu.foo:
import org.gnu.foo.Bar;
class ClientCode {
public static void main(String[] args) {
Bar bar = new Bar();
bar.someMethod(args[0]);
}
/* ... */
}
or import org.gnu.foo.Bar;
class MoreClientCode {
void clientMethod(Bar bar) {
bar.someMethod("xyzzy");
}
/* ... */
}
- Extend from a type in org.gnu.foo:
import org.gnu.foo.Bar;
class ClientCode extends Bar {
/* ... */
}
- Specialize a type from org.gnu.foo, but via composition rather than inheritance:
import org.gnu.foo.Bar;
class ProxyBar {
ProxyBar(Bar bar) {
this.bar = bar;
}
void someMethod(String str) {
return bar.someMethod(str.toUpperCase());
}
/* ... */
private Bar bar;
}
- Invoke a method from org.gnu.foo using "hard-coded" reflection:
class ClientCode {
public static void main(String[] args) throws Exception {
Class klass = Class.forName("org.gnu.foo.Bar");
Method method = klass.getMethod("someMethod", new Class[] { String.class });
Object obj = klass.newInstance();
method.invoke(obj,new String[] { args[0] });
}
}
- Invoke a method from org.gnu.foo using runtime-determined reflection:
class ClientCode {
public static void main(String[] args) throws Exception {
Class klass = Class.forName(args[0]);
Method method = klass.getMethod(args[1], new Class[] { String.class });
Object obj = klass.newInstance();
method.invoke(obj,new String[] { args[2] });
}
}
invoked via java ClientCode org.gnu.foo.Bar someMethod xyzzy
- Invoke a method from org.gnu.foo as an external process:
class ClientCode {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("java org.gnu.foo.Bar xzyyz");
/* ... */
}
}
How do these answers change if org.gnu.foo is licensed under the "regular"