• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The "main" problems !

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) { ...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all right i've decided to look further into this now . thank you anyway .
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic