• 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

Rules for main

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Is there any good source for giving comprehensive info on the usage of main
eg. I was surprised to find out programs like this work

am sure that there will be a dozen other foxy cases like these. How does JVM actually look for what to execute
Also, the above query is adapted from http://www.valoxo.ch/jr/mocks/mock01a.html q23
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ January 17, 2003: Message edited by: Jessica Sant ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is so terrible about that program?
Usage of the main method:
SCJP FAQ: Is it allowed to declare the main method private?
[ January 16, 2003: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
Just to make sure...The code is giving a compilation error. I am using java 1.4. i guess we have to declare the class which contains the method to be public . right ?
Sri
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is correct.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make sure...The code is giving a compilation error.
save this file as Q123.java, you get a compiler error, <- because class test defined as 'public', the file should be saved as test.java
If save as test.java compiles OK, but give
runtime error. <- no main method defined inside
class test.

I am using java 1.4. i guess we have to declare the class which contains the method to be public . right ?
Not necessary, remove the 'public' modifier on test, save as Q123.java, this way, without any public modifier the program will runs OK.
[ January 17, 2003: Message edited by: chi Lin ]
[ January 17, 2003: Message edited by: chi Lin ]
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Layne and Chi
Sri
 
Harpreet Kaur
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies for the confusion
a) the file should be named test.java, as pointed out earlier
b) I am using Jcreator as an IDE. If I use the compile file and execute file options, the program executes. If I use cmd line on Windows
java test (with test in java sdk's bin)
I get an exception
Exception in thread "main" java.lang.NoSuchMethodError: main
any suggestions. I used to think Jcreator will call the usual cmd line. However, this does not appear to be the case. Am I missing anything?
c) This is for Valentin Crettaz
Thanks for your tests.
The problem that I keep on having with main is that I just know one single rule
that if there is a method named main,
it should have a signature of public static void main (Strings args[]))
There was an implicit assumption that main will be mentioned only once in a public top level class
That assumption proved to be untrue.
There may be other issues eg. if a child and parent class both have main (with child as public)
can the child's main invoke the parent's main
(or vice versa).
Similarly, can main be a part of an interface?
is it ok if a public top level class also implements that interface?
Of course, I can try this out by compiling.However, I guess there would be a rule (or a couple of them) out there that will cover all the possible cases and prevent any surprises.
Thanks for the help and for having hte patience to read until this point :-)
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

b) I am using Jcreator as an IDE. If I use the compile file and execute file options, the program executes. If I use cmd line on Windows
java test (with test in java sdk's bin)
I get an exception
Exception in thread "main" java.lang.NoSuchMethodError: main


JLS12.1: "JVM starts execution by invoking the method main of some specified class". So, when you do java test, test class doesn't have main method, instead Q123.class has the main method. java Q123 will work.
I didn't use Jcreator, but from my experience with JBuilder, it will sift through files in your project for main method/s and locates it in Q123, and it is what Jcreator is using to execute your program. If you have an option to turn on output window, it will show the exact command it is executing. It will show java Q123.
-Shiva
[ January 17, 2003: Message edited by: Shiva Mantri ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is for Valentin Crettaz
Thanks for your tests.

You are welcome
The problem that I keep on having with main is that I just know one single rule
that if there is a method named main,
it should have a signature of public static void main (Strings args[]))

Correct
There was an implicit assumption that main will be mentioned only once in a public top level class
Wrong
That assumption proved to be untrue.
Correct
There may be other issues eg. if a child and parent class both have main (with child as public)
can the child's main invoke the parent's main
(or vice versa).

Yes, it can. The main method is just another static method. There is nothing special about it, except that the JVM will look for and run the main method when an application is started.
Similarly, can main be a part of an interface?
No because an interface can only declare abstract methods and since abstract methods cannot be static, there cannot be any static method in an interface.
is it ok if a public top level class also implements that interface?
See the previous answer
Thanks for the help and for having hte patience to read until this point :-)
Sure
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic