1 2 Previous Next 25 Replies Latest reply: Nov 15, 2006 10:52 AM by mlk RSS

    why do we need static for main() .........?

    807607
      Why do we need static for main method

      class Sample
      {
      public static void main(String args[])
      {
      System.out.println("HI EVERYONE");
      }
      }
        • 1. Re: why do we need static for main() .........?
          800322
          Because the JLS says so, and:

          which object should execute main() if there isn't one created?
          • 2. Re: why do we need static for main() .........?
            807607
            And of course you can't create an object until the program starts.

            Which came first, the chicken or the egg?
            • 3. Re: why do we need static for main() .........?
              800322
              And of course you can't create an object until the
              program starts.
              class Test {  
                static {
                  new Test().main();
                }
              
                public void main() {
                  System.out.println("I only have problems with command-line args");
                }
              }
              • 4. Re: why do we need static for main() .........?
                masijade
                As Rene alluded to, without actually saying it, it is because when the JVM executes your class it effectively does it as follows:
                Sample.main();
                not
                Sample s = new Sample();
                s.main();
                Edit:

                I overlapped your last post Rene.
                • 5. Re: why do we need static for main() .........?
                  800322
                  As Rene alluded to, without actually saying it, it is
                  because when the JVM executes your class it
                  effectively does it as follows:
                  Indeed. It could do the following, though:
                  new Whatever(theArgs);
                  But that would enforce you to provide an appropriate c'tor, which is in no way better than having an appropriate static method.
                  • 6. Re: why do we need static for main() .........?
                    807607
                    We need static for main() for the following reason:

                    main() method is called by Java interpreter before any object are created. Whenever we need to call a method of a class, we need to create and instantiate n object of that class

                    For example

                    class A {
                    b(){ 
                    ...........
                    }
                    }

                    now, A obj = new A();
                    obj.b();

                    But in a class which in the entry point of your program where main() resides we can't do that because main() is called before any object creation.

                    Static keyword allows a method to be called without having to instantiate any particular instance of that class. So, we use static main().

                    Hope that helps you. cheers ;)
                    • 7. Re: why do we need static for main() .........?
                      800322
                      Java interpreter
                      There is no Java interpreter. At least not in Sun's JVM.
                      • 8. Re: why do we need static for main() .........?
                        807607
                        Java interpreter
                        There is no Java interpreter. At least not in Sun's JVM.
                        -------------------------------------------------------------------------------------
                        Java Interpreter is a synonym of Java application launcher and has purpose to execute bytecode for the JVM.Am i right? Listen, a suggestion .It doesn't matter what i say, 10 years ago's interpreter, or your current JIT compiler.Main is the answer i gave.And can you prove
                        any Java tools does not have interpreter? Is that became a Dino now ?

                        So, you are here to give answer of that question. Ask the questioner who made him cleared about his question.
                        • 9. Re: why do we need static for main() .........?
                          mlk
                          Java Interpreter is a synonym of Java application
                          launcher and has purpose to execute bytecode for the
                          JVM.Am i right?
                          I'd say no. An interpreter would take source code and run it without compiling, like BeanShell. A VM is a "pretend computer" that takes that computers native commands (in this case Java byte code).
                          Listen, a suggestion .It doesn't
                          matter what i say, 10 years ago's interpreter, or
                          your current JIT compiler.Main is the answer i
                          gave.And can you prove
                          any Java tools does not have interpreter? Is that
                          became a Dino now ?
                          :huh:
                          So, you are here to give answer of that question. Ask
                          the questioner who made him cleared about his
                          question.
                          I have no idea what you mean.
                          • 10. Re: why do we need static for main() .........?
                            807607
                            The Java virtual machine is another term for the Java interpreter, which is the code that ultimately runs Java programs by interpreting the intermediate byte-code format of the Java programming language. The Java interpreter actually comes in two popular forms: the interpreter itself (called java) that runs programs via the command line or a file manager, and the interpreter that is built into many popular Web browsers such as Netscape, HotJava, and the appletviewer that comes with the Java Developer's Kit. Both of these forms are simply implementations of the Java virtual machine, and we'll refer to the Java virtual machine when our discussion applies to both. When we use the term java interpreter, we're talking specifically about the command line, standalone version of the virtual machine; when we use the term Java-enabled browser (or, more simply, browser), we're talking specifically about the virtual machine built into these Web browsers.

                            excerpts Orelly "Java Threads" Chapter 1. (If you don't believe me). Did i do any fundamental wrong by saying interpreter ? Is that a Sin ?

                            That is why i told concentrate on what answer I gave regarding the question. I am not going to argue any more. If you don't like my comments then I am Sorry :(
                            • 11. Re: why do we need static for main() .........?
                              mlk
                              The Java virtual machine is another term for the Java
                              interpreter,
                              I would say "interpreter" is more commonly used for "interpreted" languages where no "compile" step is used (such as JavaScript). As such I would not use "interpreter" here.
                              forms: the interpreter itself (called java) that runs
                              programs via the command line or a file manager, and
                              the interpreter that is built into many popular Web
                              Not a fan of that wording. You don't have two JVMs. It is not really "built in" to web browsers, but a plugin creates a JVM instance.
                              excerpts Orelly "Java Threads" Chapter 1.
                              Is that a Sin ?
                              Never read that book, but O'Reilly books are normally good, however I would avoid "interpreter", but it is almost-kind-of correct.
                              • 12. Re: why do we need static for main() .........?
                                800322
                                Bytecode isn't interpreted but compiled into native code by the JIT compiler and then normally executed. That's my point.
                                • 13. Re: why do we need static for main() .........?
                                  807607
                                  Yes, may be we are having a mis communication. ;). Of course compiled Java or Javascript are different but lots of guys use Java interpreter. i may not be as knowlegeble as they are.

                                  Another man/book :

                                  Java2,The Complete Reference - Herbert Schild , uses the word Java interpreter.which is a core Java introductory book.

                                  Hope he is not wrong too.
                                  • 14. Re: why do we need static for main() .........?
                                    mlk
                                    Bytecode isn't interpreted but compiled into native
                                    code by the JIT compiler and then normally executed.
                                    That's my point.
                                    I thought only key sections of code were compiled to native.
                                    1 2 Previous Next