12 Replies Latest reply: Jun 3, 2007 2:36 PM by 807600 RSS

    main in a class?

    807600
      is main supposed to be in a class? in c++ main is in nothing, right? why should it be in a class? do you use this class for anything else than having main in it?

      the code still starts with main right?
      it starts with:
      public static void main(String[] args)
      right?
        • 1. Re: main in a class?
          807600
          Check the following link,

          http://www.dickbaldwin.com/java/Java014.htm

          http://www.telecom.ece.ntua.gr/HTML.Tutorials/java/anatomy/main.html

          http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

          Good luck
          Ram102125.
          • 2. Re: main in a class?
            807600
            is main supposed to be in a class? in c++ main is in
            nothing, right? why should it be in a class? do you
            use this class for anything else than having main in
            it?
            You know that Java is not C++, right? So why assume everything will be the same?

            You can use this class with main for other things if you wish. Personally, for anything other than a trivial bit of code, I don't, but that's just me. A class should have a single, clear responsibility. As far as I'm concerned, "kicking off proceedings" falls under the heading of a single clear responsibility. One of the big mistakes newbs make - as you'll see in posts here - is doing everything in main
            the code still starts with main right?
            it starts with:
            public static void main(String[] args)
            right?
            Yep
            • 3. Re: main in a class?
              807600
              i'm not assuming everything is the same. i just dont understand why main is in a class. since main is the main code (i assume this due to its name).
              • 4. Re: main in a class?
                807600
                i'm not assuming everything is the same. i just dont
                understand why main is in a class. since main is the
                main code (i assume this due to its name).
                I think that in java anything that has programming statements in it (not package, import, ...) has to be in a class or a class equivalent (i.e., enum).

                I could be wrong though. It is a good question though that forces one to analzye basic assumptions.
                • 5. Re: main in a class?
                  807600
                  I think that main must be in a class because this is simply how it is defined in Java.

                  I looked at the Java specifications which state A Java Virtual Machine starts up by loading a specified class and then invoking the method main in this specified class.

                  The only way the java virtual machine can find the main method is by being told at startup the name of the class that contains it.
                  • 6. Re: main in a class?
                    807600
                    Had you taken the 30 seconds necessary to write a class, compile it and then run it (without a main method in it), then you would have answered your own question. Each of the following examples took me a grand total of about 30 seconds to write, compile and execute.

                    First test:
                    class Test { }
                    Second test:
                    public static void main(String[] argv) { // this is stupid }
                    
                    class Test { }
                    The error messages that you receive tell you everything you need to know.
                    • 7. Re: main in a class?
                      807600
                      this is not logical. and i still haven't found the answer. (aside from the previous post)
                      • 8. Re: main in a class?
                        3004
                        Any executable code in Java must be part of a class. I assume the reason it was done that way is to keep things simple and consistent.
                        • 9. Re: main in a class?
                          807600
                          The way I see it, by associating the main method with a class (as opposed to a project/program), you can use several main methods within your project for bug testing individual pieces and there's no issues. Might make a security flaw if you leave them in there, but you could always remove them after the bug testing is complete. shrug
                          • 10. Re: main in a class?
                            807600
                            Main can be either a class of its own or in a separate class. If you click the button that says "make this your main class" then that class will contain the main args[] statement. Or you can just create a class, call it main or anything else and have that statement in it. The main class is where everything starts in Java.
                            • 11. Re: main in a class?
                              807600
                              If you click the button that says
                              "make this your main class" then that class will
                              contain the main args[] statement.
                              I didn't know that this was a specification of the java language. Shoot. I've always been using a text editor. Where's that button been hiding? :p
                              • 12. Re: main in a class?
                                807600
                                Main can be either a class of its own or in a
                                separate class. If you click the button that says
                                "make this your main class" then that class will
                                contain the main args[] statement. Or you can just
                                create a class, call it main or anything else and
                                have that statement in it. The main class is where
                                everything starts in Java.
                                ???

                                Anyway ... @OP: What about this isn't logical? I, personally, think that it is a great aspect of the language.