Saturday, December 18, 2010

Calling main method inside main method java

Here is the way for $subject.

import java.util.*;

public class Main {

 public static void main(String[] args) {
  System.out.println("Hi I need to call main inside main");  
  System.out.println("Do you need to exit? (y|n)");
  
  Scanner scanner=new Scanner(System.in);
  String s= scanner.next();
  char ans=s.charAt(0);
  
  switch(ans){
  
  case 'y':System.exit(0);break;
  case 'n':String[] argz={"A","B"};
  main(argz);
  
  }

 }

}



Note: if you need to invoke a main method of another class it should be done through the name of that particular class as main is a static method.

Output

No comments :

Post a Comment