Вопрос

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.

Другие советы

There is no need of forward declaration, just write the functions.

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();
   }

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top