Your Ad Here

Why can’t Java methods be first class objects?

I’m not trying to sound smart or be a computer science elitist — I’m just asking.

In languages like Scheme (ergo LISP), and others, functions (analogous to Java methods) are “first class/higher order objects”. For the purposes of this post, the great benefit of this is the ability to pass a function as an argument to another function.

Now, why, you may ask, would you want to do that?

I was working on an assignment last night, which had to be done in Java, and came across a case where I really wanted to do this.

The project was a doubly-linked list, ostensibly to store employee records. It turned out there were a lot of cases where I would need to loop through the list:

  • Part of the requirement was to store the nodes in alphabetical order by last name. To do this, all you need to do is search the existing list until you find a node with a last name “greater” than the one you want to insert (java.lang.String has a method call compareToIgnoreCase() which does this quite well — for English, at least, since the comparison is to the order of unicode characters).
  • Another thing you might need to do is delete a record by the employeeID — no problem, just search the list for a specific ID.
  • By extension, since employee ID needed to be unique, you also needed to search the list each time you added a node, to make sure the employee ID did not already exist — if it did, you can’t allow the new node to be added.
  • Another requirement was to be able to simply print the whole list — again, just loop through the list and print each record.
  • Yet another task was to print a specific employee by first and last name — so, just loop through, and any record(s) which match the given first and last name, print them.

So I had at least 5 different functions, each of which required loop, potentially a loop all the way through the list.

Wouldn’t it be nice to just write one method that would loop through the list, instead of 5 different ones?

Well, yes, it would be nice… except that the actual process that needs to be performed in each case needs to take place inside the loop. So, if you want to have an all-purpose loop method, you need a way to somehow “inject” different code into the middle of it.

Well, if you could pass methods (functions) as arguments, you could just call the loop method with a certain function as an argument and have it execute that function within the loop. That would be neat.

To be fair, it sounds as if you can do something like this in Java, with something called functors. Unfortunately, I did not find very much information on actually implementing functors, and I couldn’t justify spending a lot of time working on that when I could easily complete the assignment just by letting each method have it’s own loop.

I gave an attempt at creating a method with a generic return type which would do different functions, via a switch-case statement, depending on the parameters passed to it. However, this admittedly crufty solution soon became more complicated than I could justify — so I caved in and wrote five different loops.

Oh well.