0

In Java, is there a way to emulate the first-class functions in lua and python, such as here?

def foo():
    print "foo called"
bar = foo
bar()      #output from this line is "foo called"

Or is the only way to have a switch statement with the different methods at each case?

Edit: thank you for the clarification, what I mean to ask is is there a way to create a reference to a function, and call it as if it was the function.

Also, the goal is not to call a certain implementation of a function, it is choose which function to call, the choices of which can entirely different. The reason I want to do this is to have a more general class that does not have to be edited to add functionality. Kind of like putting functions into a collection or list.

Edit2: for anybody viewing this question for the purpose of finding an answer to it: This has to do with stacks and heaps. Java cannot do this because its methods are stored on the stack, which is much more rigid than the heap. Python, however, stores its functions on the heap, with a reference to them on the stack. Since functions are called through a reference in python, you can change the reference to them and use as desired.

Mitchell Carroll
  • 479
  • 5
  • 13
  • What are you switching on? It's frequently possible to do this more nicely with an `enum`. – Louis Wasserman Sep 30 '13 at 19:19
  • 1
    Here's some [literature](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) for you. – Mena Sep 30 '13 at 19:19
  • 2
    no, create an interface and have multiple implementations of the interface. – jtahlborn Sep 30 '13 at 19:19
  • 1
    @JohnKugelman It's first **class** functions, and a related term is **higher** order functions. – Marko Topolnik Sep 30 '13 at 19:29
  • Usually classes implementing the interface [`Runnable`](http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html) are used as a substitute for first class functions. The objects themselves can then be passed around obviously. – dualed Sep 30 '13 at 22:02

3 Answers3

3

The code in the question does not demonstrate pass-by-reference behavior. In fact, neither Java nor Python have pass-by-reference semantics, it's all pass-by-value.

What you're demonstrating is first-order functions: the ability to treat a function as any other value. In Java 7 and older this is not possible, although in Java 8 a new syntax for anonymous functions (called lambdas) was introduced. The lambda syntax can be used to imitate the behavior shown.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

you can't simulate pass by reference for methods, for similar things use interfaces, but for objects you can simulate here

Community
  • 1
  • 1
Eugen Halca
  • 1,775
  • 2
  • 13
  • 26
0

(As @Óscar López just posted as I was writing this) What you're doing is keeping a reference to a function and passing that around by value.

The "Java way" of doing this would be to use interfaces:

Interface:

public interface Printer {
    public void doPrint();
}

Implementation:

public class FooPrinter implements Printer {
    public void doPrint() { System.out.println("foo"); }
}

(Other implementations could be done - BarPrinter, BazPrinter, etc.)

Caller:

Printer p = new FooPrinter();
// presumably some other code here and
// probably p is passed to another function which accepts Printer (not FooPrinter)
p.doPrint(); // outputs "foo"
Brad Peabody
  • 10,917
  • 9
  • 44
  • 63
  • My issue though is that I want the program to be a template for different behaviors: eg. I might not know that the function is called doPrint, or that doPrint is the function I want to call. What I do know is the amount and types of all parameters and returns. Essentially I want to know if there is an implementation of the ability to change the functionality of a program at runtime on a level deeper than command line args and file input. – Mitchell Carroll Sep 30 '13 at 23:21
  • If you know the arguments and the return type - then what difference does it make what the call is named? Call it "doAction"... – Brad Peabody Sep 30 '13 at 23:43
  • But otherwise, to do what you want at the language level, sounds like you want something like Groovy. – Brad Peabody Sep 30 '13 at 23:43