How do I declare a function at runtime?
This is something like Reflection , where you can call a method simply by its name, rather than a precompiled pointer to it.
As with JavaScript, you can:
var fun = function(){ alert("Hello World!"); }
fun();
Is something like this possible in Java / J2ME? How?
source to share
If you need an interpreter for j2me you can check out Hecl: http://www.hecl.org
You definitely can't do most of the "fancy" things you can do in regular Java in J2ME. An alternative is the if / elseif series, which then calls the function you want, but you still need to know what to do ahead of time so you can write the code that does it.
source to share
One way to mimic this functionality is to create a strategy class.
interface Function
{
Object call(Object[] arguments);
}
To do what you suggested, simply do the following (somewhat more verbose, as usual in Java):
static class Fun implements Function
{
public Object call(Object[] arguments)
{
System.out.println("Hello, world");
return null;
}
}
Function fun = new Fun();
fun.call(null);
Depending on the situation, you can use better quality types or generics instead of Object
and Object[]
(in this case I used them for maximum flexibility, but they don't give you much in the way of type checking, so it's not ideal).
For more information, see the Strategy Design Template .
source to share
A common way to get something like Javascript closure is to use anonymous inner classes. This is much more detail, but it allows you to do almost the same thing.
Runnable r = new Runnable(){
public void run(){
System.out.println("Hello, world!");
}
};
r.run(); // Prints "Hello, world!"
You can even reference variables in your application if they are final:
public static Runnable makeGreeter(final String who) {
return new Runnable() {
public void run() {
System.out.println("Hello, " + who + "!");
}
};
}
// ... elsewhere in your program...
Runnable r = makeGreeter("world");
r.run(); // "Hello, world!"
This is the standard stuff that has been in Java since the beginning. Runnable
is a very user-friendly interface which, according to the Javadocs, "must be implemented by any class whose instances are intended to be executed by a thread." Runnable
can be used for much more than streams, and is generally used (in the JVM and elsewhere) as "something that can be done" - almost like a function in Javascript. Of course, if you want to pass arguments, you'll have to create your own interface, but they can also be implemented anonymously. For example, using the @Imagist Function
interface :
interface Function {
Object call(Object[] arguments);
}
// ...
Function helloSayer = new Function(){
public Object call(Object[] args){
System.out.println("Hello, " + args[0] + "!");
}
};
helloSayer.call(new Object[]{ "world" }); // "Hello, world!"
Edit: This has nothing to do with thinking, of course, but there is no reflection in your example - just an anonymous function.
source to share
Are you after dynamically creating methods or Reflection ?
The first thing you cannot do in Java, but you can use an interpreter ( BeanShell if you want Java).
source to share
There are at least several ways:
You can create classes at runtime with BCEL , but you must create bytecodes yourself. And the JVM verifier may reject your class if your bytecodes look like iffy. Not a very simple solution, but doable.
Sun's Java 6 implementation includes Rhino , a JavaScript interpreter, so you can do what you want if you want to use a little JavaScript.
I'm not too sure about this, but I believe that if you have the JDK installed, you can call javac
from your Java program. javac
the output can be loaded at runtime using a custom class ClassLoader
.
source to share