简体   繁体   中英

How to have two functions that call each other Java

i know we do this in C++ like this :

int a();
int b() { 
  return a();
}
int a() { 
  return b();
}

how i can do something like this in Java ?

In Java you don't have to declare variables or functions before using them. Therefore:

int b() { return a();}
int a() { return b();}

Note that this will yield a StackOverflowError .

无需前向声明,只需编写函数。

Here it is : The dangerous code for you :

public class b 
{

   Object  first()
   {
     System.out.println("i am inside first function");
     return second();
   }

   Object  second()
   {
     System.out.println(" Like i care !  i'm scared of StackOverflowError dude !!");
     return first();
   }

   public static void main(String [] args)
   {
     new b().first();
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM