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

1 comment :

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete