 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Singleton Guest
|
Posted: Wed Nov 23, 2005 4:37 pm Post subject: Hello world with main?? need help |
|
|
How do i write a java program that doesn't have any "public static void
main(String[] args)" method and prints "Hello World" to the console ?
|
|
| Back to top |
|
 |
Brett Kirkness Guest
|
Posted: Sat Dec 03, 2005 1:54 am Post subject: Re: Hello world with main?? need help |
|
|
Hi
You have to have a static void main.
you can't start any program without it
brett
"Singleton" <kuchbhi (AT) homecall (DOT) co.uk> wrote
| Quote: | How do i write a java program that doesn't have any "public static void
main(String[] args)" method and prints "Hello World" to the console ?
|
|
|
| Back to top |
|
 |
Errol Miller Guest
|
Posted: Thu Dec 08, 2005 12:41 am Post subject: Re: Hello world with main?? need help |
|
|
This article shows another method. I got it from:
http://www.devx.com/tips/Tip/29837
Expertise: Intermediate
Language: Java
November 15, 2005
Write a Standalone Java Application Without a Main Method
Normally, you need a main method in a Java class if you want to run that
class from the command line. However, there is a little trick that allows
you to run one without a main method:
class NoMainMethod
{
static
{
System.out.println("Look ma! no main method");
System.exit(0);
}
}
The reason this works is that static initialization blocks get executed as
soon as the class is loaded-even before the main method is called. As soon
as the block exits, it will look for the main method. When it doesn't find
it, it throws an exception-so the statement exits the program before the
exception is thrown.
Sachin Kainth
"Singleton" <kuchbhi (AT) homecall (DOT) co.uk> wrote
| Quote: | How do i write a java program that doesn't have any "public static void
main(String[] args)" method and prints "Hello World" to the console ?
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|