JAVA Main method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

JAVA Main method

in java,if we return back to main method with code --> main(args) in the end of the code,the before process would be save? or it will going like new start. For example i did an array and if i call main(args) the element inside array would be clear? or saved,instead of being clear,got any idea to make the element be saved?

13th Aug 2019, 8:11 AM
Tzion
Tzion - avatar
6 Answers
+ 2
use array as class variable (instead of local method variable) public class Program { static int i=0; static int[] array = new int[10]; public static void main(String[] args) { if (i < 10) { array[i] = i; System.out.print(array[i++]+", "); main(args); System.out.print(array[--i]+", "); } else { System.out.println(); } } }
13th Aug 2019, 9:09 AM
zemiak
+ 1
Are you talking about invoking main() again from somewhere within the program?
13th Aug 2019, 8:21 AM
Sonic
Sonic - avatar
+ 1
Sonic zemiak actually i was using it in my Stack data structure program. You can check my profile i create a code name (STACK code example), in there you can see i put main(args) in the end of code,to make it return and start again,but the element in stack were gone.... thats why i got no idea how to solve it
13th Aug 2019, 11:26 AM
Tzion
Tzion - avatar
+ 1
Rael Your program doesn't end and restart when you call main, instead, it will allocate a new memory and start again without ending the last one, your last data still there but it's not accessible through the new program, your program will also crash eventually because it will fill the memory to its limit. I suggest you to not make any program that call itself, and use loop for that instead
13th Aug 2019, 1:34 PM
Agent_I
Agent_I - avatar
+ 1
nice!
13th Aug 2019, 7:52 PM
Juan Debenedetti
Juan Debenedetti - avatar
0
public class Program { static Scanner scanner = new Scanner("elm1\nNO\nelm2\nYES\nelm3\nQUIT\n"); //System.in); static Stack<String> eg = new Stack<String>(); public static void main(String[] args) { ... if(input1.equals("YES")){ ... } if(! input1.equals("QUIT") ) main(args);
13th Aug 2019, 2:21 PM
zemiak