aspose file tools*
The moose likes Java in General and the fly likes Running code without main().... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running code without main()...." Watch "Running code without main()...." New topic
Author

Running code without main()....

Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi all....

Suppose you create a file name Test.java and then add following piece of code to it-

Public Test{
static{
System.out.println("runned without main()");
System.exit(0);
}
}
And then compile it and run the output will be as expected.
So my question is can you replace main and use the above method(offcourse in some cases as it don't takes arguments). Will there be a performance hit or some other issues....
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


So my question is can you replace main and use the above method(offcourse in some cases as it don't takes arguments)

What happened when you tried it?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Did that run?
Actually jvm looks for a method which has the signature of main(String [] a)
and then calls that marking the start of the program..I do not think your class should run.


Rahul Bhattacharjee
LinkedIn - Blog
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes, it runs. The static initializer is executed while the class is loaded. At the end of the initializer, System.exit() is called, so the program exits. If you hadn't called System.exit(), of course, java.exe would next have tried to find the main() method of the loaded class, failed to find it, and exited with a diagnostic.

Is this a serious question about performance? That's kind of like asking whether flushing the toilet is going to make the oceans overflow. One tiny reflective method call is nothing compared to the time it takes to start and stop the JVM. This has no measurable impact on performance whatsoever.


[Jess in Action][AskingGoodQuestions]
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
The static block will execute your program as if you started it with a main() method (no arguments though).
It will not improve performance and will definitely make it a lot harder to understand (what do you search first for if you try to find the entry point of a program ?)


That's kind of like asking whether flushing the toilet is going to make the oceans overflow.


Will it ?

:-)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I see two implications:

- you can't pass any command line arguments to the program, and

- you could get your initialization wrong (static fields that are declared after the static initializer will get initialized when the program *finishes* :eek


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Running code without main()....