Skip to content

Instantly share code, notes, and snippets.

@sorindragan
Last active December 6, 2018 16:32
Show Gist options
  • Save sorindragan/14144a9af24ebcce8b5d4d732ad62fed to your computer and use it in GitHub Desktop.
Save sorindragan/14144a9af24ebcce8b5d4d732ad62fed to your computer and use it in GitHub Desktop.
In a recursive method print something only once.
public class recursiveMethod {
// print something only once in a recursive method
public static int y = 0;
public static boolean recursiveMethod(int x) {
if (x > 4) {
return true;
}
Exception e = new Exception();
e.fillInStackTrace();
if (e.getStackTrace().length == 2) {
System.out.println("First Time");
// print this statement only the first time
}
y++;
System.out.println(y);
e.printStackTrace();
return recursiveMethod(x + 1);
}
public static void main(String[] args) {
recursiveMethod(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment