Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The "main" problems !

 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Norm Radder
Ranch Hand
Posts: 732
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the java command to start a program, it needs a place to start. The convention is to use the main(String[] args) method. Because the class hasn't been created yet, main() must be static and public so a program from another package can get to it. main doesn't return anything.
So you end up with:

public static void main(String[] args) { ...
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why doesn't main() return something like n C or in C++(can we make it return something ?)


This is merely a design issue. ANSI C chose to have the error code returned via the main() method. And Java chose to have the error code returned via a parameter to the exit() method call.

To find out why, you will have to question the designers of C and Java to see why they decided the way they did.

why does main have to be static , can it in any way not be made non-static ?


Again, a design decision. I guess it would have been okay to make the main method non-static, and have a default constructor for the main class. But like the previous question, you will have to question the designers of Java to see why they decided the way they did.

Henry
 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all right i've decided to look further into this now . thank you anyway .
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vaibhav panghal:
all right i've decided to look further into this now . thank you anyway .


You got the correct answers to your questions -- there's nothing further to look into, my friend.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic