-4

I know this fact that main() method is for starting point of programme which is defined by technology.But still we can do entire thing without main method any without any error. I have this code

    public class WithoutMain {

        int x=10;
        int y=20;

        void show() {
            System.out.println(x);
            System.out.println(y);
        }

        static {
            WithoutMain t=new WithoutMain();
            t.show();
            System.exit(0);
        }

    }

I can write this programme without main method, so why is it necessary to have a main method?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Arun
  • 1,038
  • 2
  • 12
  • 25

4 Answers4

1

As far as I know, your application isn't going to start without a main method. Not even with the trick you wrote there. Did you actually try it?

The JLS (§8.7) says:

8.7. Static Initializers

A static initializer declared in a class is executed when the class is initialized (§12.4.2). Together with any field initializers for class variables (§8.3.2), static initializers may be used to initialize the class variables of the class.

Since there is no entry point, no single class is loaded, which implies that the class with the static initializer doesn't load. So the static initializer doesn't invoke.


Other information:

If you don't create a main method, your program has no entry point. This is used when someone writes a library (eg: Java Mail). The library itself doesn't have to start. It is the application that uses the library that has to create a main method.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • It should work though. Since he/she is exitting the system with System.exit(0). The JVM does not get chance to check main method since it attempts to initialize the static block and exits within the static block. However, this will not work if you remove the exit statement. – Jimmy Oct 15 '12 at 17:43
  • There is no reason for the JVM to execute the static initializer. The call to System.exit(0) won't change a thing. – Martijn Courteaux Oct 15 '12 at 17:46
  • @MartijnCourteaux Not sure - see http://stackoverflow.com/questions/11421542/how-can-you-run-a-java-program-without-main-method – assylias Oct 15 '12 at 17:46
  • Ah, yes that is true. But that is because you explicitly told the JVM to load the class by invoking it like this through the command line: `java WithoutMain`. But if you create a standalone application, it won't work. – Martijn Courteaux Oct 15 '12 at 17:51
  • Run this code ( from command window), run it again removing the System.exit statement. You will see the difference. public class Try { static{ System.out.println("It works"); System.exit(0); } } – Jimmy Oct 15 '12 at 17:51
1

The main(String[] argv) lets you pass parameters in and return a value... Doing it with a static block doesn't.

Kevin Rubin
  • 587
  • 8
  • 17
1

you can run program without main() but you cant run exe or jar file without entry point.
the main method is the default entry point for a programme,you can run independently jar files.
If you're not trying to produce a programme that needs launching independently, you won't need it - for example, a jar referenced by other programmes, or a website.
by help of this main() you can pass arguments

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • Thank you..I did not think about jar where entry point will play an important role..Thank u so much – Arun Oct 15 '12 at 17:47
0

In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application.

Frank
  • 16,476
  • 7
  • 38
  • 51