i have here some interesting code snippets that someone just asked me to explain . They might interest some of you too and please tell me the concept.
1 ) code without main()
class First
{
static{
System.out.println("this is a program without main...");
System.exit(0);
}}
the above executes perfectly !
2 ) code with two main()s
class First
{
public static void main()
{
System.out.println("it is the overloaded vesion of main...");
}
public static void main(
String [] ar)
{
System.out.println("invoking overloaded main..");
main();
}}
output
invoking overloaded main..
it is the overloaded version of main.....
3 ) in this one the sequence of calling changes from the above code
class First
{
public static void main()
{
System.out.println("haha");
}
public static void main(String [] arr)
{
System.out.println("hehe");
}
static{ main(); }
}
output
haha
hehe
There are two more questions i'd like to ask - why doesn't main() return something like n C or in C++(can we make it return something ?) and secodly , why does main have to be static , can it in any way not be made non-static ?