Last active
December 6, 2018 16:32
-
-
Save sorindragan/14144a9af24ebcce8b5d4d732ad62fed to your computer and use it in GitHub Desktop.
In a recursive method print something only once.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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