33

Is it possible to define a function within a function in Java? I am trying to do something like:

public static boolean fun1()
{
  static void fun2()
  {
     body of function.
  }
  fun();
  return returnValue;
}

but I am getting error Illegal start of expression.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Harshveer Singh
  • 4,037
  • 7
  • 37
  • 45
  • 4
    what are you trying to accomplish that need nested methods? – Hunter McMillen Aug 17 '11 at 18:01
  • the code of function2 appears three times in fun1 in my code (It is an big function for matching stock-exchange orders(my school assignment))and it uses many local variables of fun1. – Harshveer Singh Aug 17 '11 at 18:13
  • so what is the problem with having two non-nested methods? Then just call function2 as many times as you want inside function1 – Hunter McMillen Aug 17 '11 at 18:14
  • There is simply no such thing in Java as a Function. There are only Objects and methods which are wholly owned by objects and subordinate to them. The object is your lord and master in java, nothing happens but through his will. – Affe Aug 17 '11 at 18:19
  • @Affe - except of static methods... – MByD Aug 17 '11 at 18:29
  • 1
    @Hunter if i define fun2 outside of fun1 then it will not be able to modify variables of fun1. when i compile it will give an error that cannot find symbol – Harshveer Singh Aug 17 '11 at 18:39
  • One could say Java represents even the class definition itself as just another type of object to the programmer :) – Affe Aug 17 '11 at 18:40
  • 1
    @Harsh, just pass the variables you need as arguments to function2 – Hunter McMillen Aug 17 '11 at 18:58

2 Answers2

50

The reason you cannot do this is that functions must be methods attached to a class. Unlike JavaScript and similar languages, functions are not a data type. There is a movement to make them into one to support closures in Java (hopefully in Java 8), but as of Java 6 and 7, it's not supported. If you wanted to do something similar, you could do this:

interface MyFun {
    void fun2();
}

public static boolean fun1()
{
  MyFun fun2 = new MyFun() {
      public void fun2() {
          //....
      }
  };
  fun2.fun2();
  return returnValue;
}
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
45

You cannot (and in Java they are called methods).

You can, however, define an anonymous class inside of a method, and call its methods.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
MByD
  • 135,866
  • 28
  • 264
  • 277
  • 6
    As I see it, there is a difference between methods and functions, while functions live by themselves, methods are related to class or object. – MByD Aug 17 '11 at 18:01
  • 2
    The clarification is useful since all java documents use this notion. Harsh (or anyone else) will find answer to future questions about methods easier with correct naming. – rics Aug 17 '11 at 18:07
  • 1
    He can, but not in `static` http://stackoverflow.com/questions/5388584/does-java-support-inner-local-sub-methods – Yousha Aleayoub Sep 07 '16 at 13:59
  • 3
    @YoushaAleayoub - Thanks, this answer was given before Java 8, and now we got lambdas :) – MByD Sep 07 '16 at 15:34