Domanda

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 ?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top