Take the 2-minute tour ×

Here is a factory "inner class?" from the Java Jung graph package:

   Factory<Graph<String, Integer>> graphFactory = new Factory<Graph<String, Integer>>()
       public Graph<String, Integer> create()
           return new SparseMultigraph<String, Integer>();

What I want to know is what programming language concept is the above? Especially what is this concept in Java? Is the above an inner class? It is clearly not a method. It seems odd to me because it constructs a Factory object and then has braces with a semicolon at the end with a method to create a graph.

share|improve this question
The word is Anonymous Inner Class. Now you can start the google process to learn more about them –  Rohit Jain Dec 29 '12 at 17:43
It is an anonymous Inner class with a function inside it. –  Android Killer Dec 29 '12 at 17:45
You could ask what is the advantage of having Anonymous Inner Class for this purpose. –  sura Dec 29 '12 at 17:50
add comment

4 Answers

up vote 4 down vote accepted

It is an anonyous inner class. The above code creates a subclass of the Factory class (or a class implementing the Factory interface), overrides its create() method, calls its constructor, and assigns the result to the graphFactory variable.

share|improve this answer
add comment

It is an anonymous inner class.

share|improve this answer
it can be a comment. –  Android Killer Dec 29 '12 at 17:44
right, should be a comment. :-/ –  Daniel M. Dec 29 '12 at 17:45
I think answer for the question is this. –  sura Dec 29 '12 at 17:46
@DanielM. Why? It answers the question. –  Tony Ennis Dec 29 '12 at 17:47
@TonyEnnis yeah it is an answer but really short. ;-) –  Daniel M. Dec 29 '12 at 18:56
add comment

It's an anonymous inner class concept.In layman terms,it can also be called an unnamed class.There is always a debate on the use of such classes against inheritance.Usually,if there is a one time usage requirement of a child class,then anonymous classes are more handy than inherited classes.

share|improve this answer
"Layman's terms" surely or is it lame? –  Reimeus Dec 29 '12 at 19:04
You are right..it's "layman" indeed.. –  Priyanka.Patil Dec 29 '12 at 19:08
add comment

As per the post it seems that you are aware that it is the concept of anonymous class in java that you are talking about. Now since you are questioning y this : The answer is that , as the name implies the class has no name, hence in a single step class declaration the creation of an instance of the class is complete. Due to this reason use of anonymous class save time and effort for creating a .java file. :) As a rule anonymous class must implement all the abstract methods in the super class or the interface and should use all the default constructor of the super class.

share|improve this answer
add comment

Your Answer


