Is it even possible?
-
7Note that this is now possible in Java 8 -- see the answer about Lambda Expressions by Mark Rotteveel below. – Josiah Yoder Jan 13 '15 at 21:06
6 Answers
if you mean an anonymous function, and are using a version of Java before Java 8, then in a word, no. (Read about lambda expressions if you use Java 8+)
However, you can implement an interface with a function like so :
Comparator<String> c = new Comparator<String>() {
int compare(String s, String s2) { ... }
};
and you can use this with inner classes to get an almost-anonymous function :)
-
7Not yet. In Java 7 it is going to be possible: http://stackoverflow.com/questions/233579/closures-in-java-7 – Ilya Boyandin May 03 '10 at 07:58
-
2In the meantime, while waiting for JDK7, anonymous methods can be emulated in an OO context using http://en.wikipedia.org/wiki/Command_pattern – gpampara May 03 '10 at 11:26
-
1
-
5I think you should modify your answer as we have anonymous function with Java 8. – Node.JS Apr 23 '14 at 13:42
Here's an example of an anonymous inner class.
System.out.println(new Object() {
@Override public String toString() {
return "Hello world!";
}
}); // prints "Hello world!"
This is not very useful as it is, but it shows how to create an instance of an anonymous inner class that extends Object
and @Override
its toString()
method.
See also
Anonymous inner classes are very handy when you need to implement an interface
which may not be highly reusable (and therefore not worth refactoring to its own named class). An instructive example is using a custom java.util.Comparator<T>
for sorting.
Here's an example of how you can sort a String[]
based on String.length()
.
import java.util.*;
//...
String[] arr = { "xxx", "cd", "ab", "z" };
Arrays.sort(arr, new Comparator<String>() {
@Override public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});
System.out.println(Arrays.toString(arr));
// prints "[z, cd, ab, xxx]"
Note the comparison-by-subtraction trick used here. It should be said that this technique is broken in general: it's only applicable when you can guarantee that it will not overflow (such is the case with String
lengths).
See also
- Java Integer: what is faster comparison or subtraction?
- Comparison-by-subtraction is broken in general
- Create a Sorted Hash in Java with a Custom Comparator
- How are Anonymous (inner) classes used in Java?

- 1
- 1

- 376,812
- 128
- 561
- 623
-
5The majority of another occurences can be found as [`EventListener`](http://java.sun.com/javase/6/docs/api/java/util/EventListener.html) (sub)implementations in the average Swing application. – BalusC May 02 '10 at 23:47
-
-
@BalusC: stackoverflow recently added the `Linked` sidebar, so I'm doing my best to make use of it. – polygenelubricants May 03 '10 at 00:42
With the introduction of lambda expression in Java 8 you can now have anonymous methods.
Say I have a class Alpha
and I want to filter Alpha
s on a specific condition. To do this you can use a Predicate<Alpha>
. This is a functional interface which has a method test
that accepts an Alpha
and returns a boolean
.
Assuming that the filter method has this signature:
List<Alpha> filter(Predicate<Alpha> filterPredicate)
With the old anonymous class solution you would need to something like:
filter(new Predicate<Alpha>() {
boolean test(Alpha alpha) {
return alpha.centauri > 1;
}
});
With the Java 8 lambdas you can do:
filter(alpha -> alpha.centauri > 1);
For more detailed information see the Lambda Expressions tutorial

- 100,966
- 191
- 140
- 197
-
2Method references are also useful. e.g. sort(String::compareToIgnoreCase) http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html – Josiah Yoder Jan 13 '15 at 21:10
Anonymous inner classes implementing or extending the interface of an existing type has been done in other answers, although it is worth noting that multiple methods can be implemented (often with JavaBean-style events, for instance).
A little recognised feature is that although anonymous inner classes don't have a name, they do have a type. New methods can be added to the interface. These methods can only be invoked in limited cases. Chiefly directly on the new
expression itself and within the class (including instance initialisers). It might confuse beginners, but it can be "interesting" for recursion.
private static String pretty(Node node) {
return "Node: " + new Object() {
String print(Node cur) {
return cur.isTerminal() ?
cur.name() :
("("+print(cur.left())+":"+print(cur.right())+")");
}
}.print(node);
}
(I originally wrote this using node
rather than cur
in the print
method. Say NO to capturing "implicitly final
" locals?)

- 145,806
- 30
- 211
- 305
-
-
@BalusC Nice catch. Actually my mistake was not to use `cur`. – Tom Hawtin - tackline May 03 '10 at 01:51
-
@Tom: +1 nice technique! Is it actually used anywhere in practice? Any name for this specific pattern? – polygenelubricants May 03 '10 at 01:51
-
@polygenelubricants Not as far as I know. Costs a whole extra object! (And a class.) Much the same was true of the double brace idiom. Right thinking people don't seem to mind the Execute Around idiom. – Tom Hawtin - tackline May 03 '10 at 02:04
-
@polygenelubricants Actually I don't seem that many (self-contained) recursive algorithms. Particularly those that are not tail-recursive (or easily made so) and cannot be implemented by calling the public method (note the slightly irrelevant `"Node" +` to make a second method necessary). / I don't have a name. Perhaps I could create a naming "poll" (CW) question, and have it downvoted into oblivion. – Tom Hawtin - tackline May 03 '10 at 02:15
-
Interesting indeed. In real one would normally do this in `Node#toString()` or similar wherein you just call the same method on left and right. I've seen `new SomeObject(){}.someMethod()` as far only on Gson's [`TypeToken`](http://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples). But that's not used recursively. – BalusC May 03 '10 at 02:16
-
@BalusC that requires you to have the algorithm implemented within the hierarchy. This demonstrates an external algorithm (I could have used arrays, say, but that would have introduced a [posh] `for` loop.) – Tom Hawtin - tackline May 03 '10 at 02:19
-
-
-
Incredible! Looks much better than separate method just for recursive calls. – barti_ddu Aug 21 '12 at 17:09
Yes, if you are using Java 8 or above. Java 8 make it possible to define anonymous functions, which was impossible in previous versions.
Lets take example from java docs to get know how we can declare anonymous functions, classes
The following example, HelloWorldAnonymousClasses, uses anonymous classes in the initialization statements of the local variables frenchGreeting and spanishGreeting, but uses a local class for the initialization of the variable englishGreeting:
public class HelloWorldAnonymousClasses {
interface HelloWorld {
public void greet();
public void greetSomeone(String someone);
}
public void sayHello() {
class EnglishGreeting implements HelloWorld {
String name = "world";
public void greet() {
greetSomeone("world");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Hello " + name);
}
}
HelloWorld englishGreeting = new EnglishGreeting();
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
greetSomeone("tout le monde");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
HelloWorld spanishGreeting = new HelloWorld() {
String name = "mundo";
public void greet() {
greetSomeone("mundo");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Hola, " + name);
}
};
englishGreeting.greet();
frenchGreeting.greetSomeone("Fred");
spanishGreeting.greet();
}
public static void main(String... args) {
HelloWorldAnonymousClasses myApp =
new HelloWorldAnonymousClasses();
myApp.sayHello();
}
}
Syntax of Anonymous Classes
Consider the instantiation of the frenchGreeting object:
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
greetSomeone("tout le monde");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
The anonymous class expression consists of the following:
The
new
operatorThe name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.
Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.
A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.
You can also use Consumer
and BiConsumer
type regarding to how many parameters you need. Consumer
accepts one parameter, BiConsumer
accepts two.
public void myMethod() {
// you can declare it here
Consumer<String> myAnonymousMethod = s -> {
System.out.println(s);
};
// you can call it here
muAnonymousMethod.apply("Hello World");
}

- 8,221
- 1
- 59
- 63