Tuesday, July 28, 2009

Why can't we use main function in main in java?

main(){


printf("main is working");


main();


}


in c language this will work and it will give infinite loop. but in the case of java


class Sample{


public static void main(String args[]){


Sample.main();


}


}


This won't work. what is the reason behind this problem?

Why can't we use main function in main in java?
First, there is no static method in class Sample with the signature you are calling (no parms). You need to pass a String array to the main method to call it. Second, you are not printing anything to screen to verify that you are indeed entering the main method. Try this:





class Sample {


static int n = 1;


public static void main(String args[]) {


System.out.println("entering main: " + n++);


String[] parms = null;


Sample.main(parms);


}


}
Reply:your call to Sample.main() should be Sample.main(null). Try:





class Sample{


public static void main(String args[]){


System.out.println("main is working");


Sample.main(null);


}


}

covent garden

No comments:

Post a Comment