I wrote down a recursive program in Java (for fun) where a few things are not clear :
public class methodTest
{
public static double methodTest()
{
System.out.println("qwerty") ; // sign that this method is called
methodTest obj1 = new methodTest() ; // creating an object (perhaps unnessary)
System.out.println(methodTest.methodTest()) ; // ??!
return methodTest() ; // returning the same function
}
}
double
, my BlueJ compiler not showing a syntax error when returning a method ?methodTest()
) and incrementing it in the method but it shows "unreachable statement"]System.out.println(methodTest.methodTest())
statement ?cannot find symbol - variable obj1
) when I replace (in System.out.println(methodTest.methodTest())
) .methodTest
with .obj1
?
Here is one way to approach this.
Notes:
public class MySampleClass
{
private static double methodTestPrivate(int callIndex)
{
//Check for terminating condition
if (callIndex >= 10)
return 42.0;
//Recursive call
return methodTestPrivate(callIndex+1);
}
public static double methodTest()
{
return methodTestPrivate(0);
}
}
public class MyProgram
{
public static void main(String[] args)
{
System.out.print(MySampleClass.methodTest());
}
}