Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am always hearing people talk about how Java does not support first class functions, that it is an advantage you get from functional languages, the only way to simulate it in Java is through the use of anonymous inner classes etc..

However, from what I can see Java does support first functions through the use of its reflection API. For example I can create a method object from a class and then call it on a number of objects of that class. I realize it is not as powerful as first class functions in other languages. For example in Python you can do the following:

class Test:
    def __init__(self, num):
        self.number = num
    def add(self, num):
        self.number += num
test = Test(1)
method = test.add

You cannot do this in Java because you need to have a reference to the object you want to invoke the method on. However you can still treat the method as an object which is what defines first class functions. I guess the method object is not really the actual function but rather a meta-data object, although using reflection it can be treated as such. Maybe I just need clarification on what defines a first class function.

share|improve this question
up vote 6 down vote accepted

A first class function is one where the function is available on its own. C, C++, and Ruby allow this approach. Java requires the function to be tied to a class and only provides a metadata representation of it, even if that class is merely a static collection of functions. C# supports first class functions with lambdas (which are based off of lambda calculus) and delegates.

Ruby is one of the languages that truly supports first class functions. The difference is that not only can you define functions on their own, but you can pass them as arguments and invoke methods on them. Check out Ruby's Proc object which is used to represent an arbitrary block of code.

The end result of having first class functions is the fact that it lends to some very powerful and flexible coding constructs. This is distinctly different than hacking around first class functions using the reflection API.

Java doesn't have full support of first class functions. The reflection API can give you some semblance of first class functions if the Method object is referencing a static method. In essence you can invoke a static method like this:

Method reference = mathClass.getMethod("sqrt");

// NOTE: the first parameter is for the object instance,
// but for static methods it is ignored
double answer = (double)reference.invoke(null, 4);

As soon as you are working with an instance method, you lose that first class function ability. You might be able to hack together some reflection based delegate support similar to C#, but the resulting code will be much slower. The "delegate" would take care of keeping the object reference for future invocations.

share|improve this answer
Yea, I see what you mean. Hmm, Is there much benefit in using that proc object over a plain old closure? – toc777 Apr 1 '11 at 16:07
In Ruby, the Proc object is implicitly created for you. It's the type of any block of code--from a full function to a lambda or a closure. Different languages have different ways of representing the same concept. The Wiki I linked to provides a feature list for first class functions (note: Java doesn't meet any of those features). – Berin Loritsch Apr 1 '11 at 16:28

the only way to simulate it in Java is through the use of anonymous inner classes etc..

This is simply wrong. Nothing prevents us from declaring a top level class that has only method(s), and treat instances of that class as functions. If the class has indeed no state, it tuns out that we need only ever one instance of it, but that is a technical detail.

Therefore, I'd say that first class functions is a language feature the language designers were aware of, and wanted to support it explicitely.

share|improve this answer
This isn't correct. Supporting first-class functions means having the ability to treat them as objects, the same way one would treat an integer or a floating-point value. You cannot assign a function to a variable in Java, nor can you pass it to a method; the closest you can do is the reflection method in Berin's answer, or through interface pointers to anonymous class instances. – jprete Apr 9 '11 at 17:48
@jprete - I have just sketched how function-objects could be implemented, and you tell me I cannot? This is ridiculous. Yet, I know what I am talking about because I am just doing that stuff in java - everything is possible: values (i.e. objects) that are functions, i.e. can be applied to other values, lazy evaluation, partial application, to name a few. – Ingo Apr 9 '11 at 19:36
Nobody needs to "sketch how function-objects could be implemented" for a language with first-class functions, because such a language already has it natively. – jprete Apr 9 '11 at 19:48
So what, @jprete? Where did I say java had first class functions? Of course they have them not! I just said that anonymous inner classes are not the only way to implement proper functions in java. I recommend "read before downvote" next time. – Ingo Apr 9 '11 at 19:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.