aspose file tools*
The moose likes Beginning Java and the fly likes The Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The "main" problems !" Watch "The "main" problems !" New topic
Author

The "main" problems !

vaibhav panghal
Greenhorn

Joined: Feb 24, 2008
Posts: 25
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

Joined: Aug 10, 2005
Posts: 690
    
    1
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
Sheriff

Joined: Sep 28, 2004
Posts: 19074
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vaibhav panghal
Greenhorn

Joined: Feb 24, 2008
Posts: 25
all right i've decided to look further into this now . thank you anyway .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The "main" problems !