Java: public static main error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #1

    Java: public static main error

    Does public static main encapsulate a class?

    Meaning:

    [CODE=JAVA]

    public static void main(String[] args) {
    // public class Myclass
    // code here to do other stuff

    // Am I encapsulating the class here?

    System.out.prin tln(className);

    }

    It probably does no make sense the way I am asking this.



    [/CODE]

    I am getting a logical error when attempting to run code. Nothing happens.

    Tell what you see. Thanks!

    Dököll
  • itgaurav198
    New Member
    • Oct 2007
    • 41

    #2
    Originally posted by Dököll
    Does public static main encapsulate a class?

    Meaning:

    [CODE=JAVA]

    public static void main(String[] args) {
    // public class Myclass
    // code here to do other stuff

    // Am I encapsulating the class here?

    System.out.prin tln(className);

    }

    It probably does no make sense the way I am asking this.



    [/CODE]

    I am getting a logical error when attempting to run code. Nothing happens.

    Tell what you see. Thanks!

    Dököll


    Have u encapsulated your main function in a class?

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Originally posted by itgaurav198
      Have u encapsulated your main function in a class?
      all attempted but nothing works. For now I have this:

      [CODE=JAVA]

      public class Game
      {
      private Parser parser;
      private Room currentRoom;
      private View currentView;



      public static void main(String[] args)
      {

      Game game= new Game();

      game.start();

      }

      [/CODE]

      Encapsulating, or having the last braket further down did not work. Above says it cannot find symbol start()

      Should the code still run using a compiler, perhaps that't it?

      I believe main is there to give it the ability to be view in a browser.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Dököll
        all attempted but nothing works. For now I have this:

        [CODE=JAVA]

        public class Game
        {
        private Parser parser;
        private Room currentRoom;
        private View currentView;



        public static void main(String[] args)
        {

        Game game= new Game();

        game.start();

        }

        [/CODE]

        Encapsulating, or having the last braket further down did not work. Above says it cannot find symbol start()

        Should the code still run using a compiler, perhaps that't it?

        I believe main is there to give it the ability to be view in a browser.
        The main method is the entry point of the program. Your game class does not have a method called start like the compiler said so you can't call it. You are also missing a closing bracket.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Dököll
          all attempted but nothing works. For now I have this:

          [CODE=JAVA]

          public class Game
          {
          private Parser parser;
          private Room currentRoom;
          private View currentView;



          public static void main(String[] args)
          {

          Game game= new Game();

          game.start();

          }

          [/CODE]

          Encapsulating, or having the last braket further down did not work. Above says it cannot find symbol start()

          Should the code still run using a compiler, perhaps that't it?

          I believe main is there to give it the ability to be view in a browser.
          What you're doing is this: a Game object encapsulates a parser, a currentRoom
          and a currentView; they're members of a Game class object. main() is just an
          entry point to get your program going. It doesn't have anything to do with browsers.
          When you start your Game class on the command line, the jvm checks whether
          or not your Game class has a main() method with the correct signature; if so,
          that's where your program starts. Your Game class doesn't have a 'start()' method
          so javac starts whining at you for that.

          I don't understand your last question: compilers have nothing to do with this.

          kind regards,

          Jos

          ps. there's a syntax error in your class: add a last '}'

          Comment

          • Dököll
            Recognized Expert Top Contributor
            • Nov 2006
            • 2379

            #6
            Originally posted by JosAH
            What you're doing is this: a Game object encapsulates a parser, a currentRoom
            and a currentView; they're members of a Game class object. main() is just an
            entry point to get your program going. It doesn't have anything to do with browsers.
            When you start your Game class on the command line, the jvm checks whether
            or not your Game class has a main() method with the correct signature; if so,
            that's where your program starts. Your Game class doesn't have a 'start()' method
            so javac starts whining at you for that.

            I don't understand your last question: compilers have nothing to do with this.

            kind regards,

            Jos

            ps. there's a syntax error in your class: add a last '}'
            Thanks to you both, I think I read the book wrong, I thought start was a reservred word/verb which when called, whatever if within the main { } would execute in a browser. That main was the engine that makes available the results of the code, when compiled, in a browser.

            So I thought having main in the code, I was having a problem when the code ran through BlueJ, the compiler I am using.

            But, just as I would for an HTML file, where I'd click and I get data through a browser, shouldn't public static void main do it. It sounds like that what the book is saying. I'll look again. I think I need to really understand what main does... But tell what you think. Thanks again for replying, guys, really nice.

            In a bit!

            Dököll

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Dököll
              But, just as I would for an HTML file, where I'd click and I get data through a browser, shouldn't public static void main do it. It sounds like that what the book is saying. I'll look again. I think I need to really understand what main does... But tell what you think. Thanks again for replying, guys, really nice.

              In a bit!

              Dököll
              You really think too much of main; it is just a static method. When you start up
              the JVM giving it a class; the JVM tries to load that class and searches for a
              static void main(String[] args) { ... } method. If it finds it it calls it given the rest
              of the command line arguments. From there the entire circus starts.

              When you want to do things in a browser you either create an Applet or JApplet
              (check the API documentation). They run on the client side in a Java enabled
              browser or you have to create 'server pages' JSPs and/or Servlets that run on a
              server and pass html pages to the browser on the client side.

              kind regards,

              Jos

              Comment

              • Dököll
                Recognized Expert Top Contributor
                • Nov 2006
                • 2379

                #8
                Originally posted by JosAH
                You really think too much of main; it is just a static method. When you start up
                the JVM giving it a class; the JVM tries to load that class and searches for a
                static void main(String[] args) { ... } method. If it finds it it calls it given the rest
                of the command line arguments. From there the entire circus starts.

                When you want to do things in a browser you either create an Applet or JApplet
                (check the API documentation). They run on the client side in a Java enabled
                browser or you have to create 'server pages' JSPs and/or Servlets that run on a
                server and pass html pages to the browser on the client side.

                kind regards,

                Jos
                Yes I think I am...

                I will be doing more reading on that. I am going to check the API documentation as you said, although I suspect it may be above my head. Will keep you posted.

                You have a wonderful week-end!

                Thanks!

                Dököll

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Dököll
                  Yes I think I am...

                  I will be doing more reading on that. I am going to check the API documentation as you said, although I suspect it may be above my head.
                  It isn't; the API documentation simply documents all the classes in the Java SE
                  core set. It also documents every method and member variable. You should
                  have the API docs open next to your IDE or whatever. There's no need to guess
                  or hack or whatever; when in doubt: read the API docs first.

                  In the Java Articles index you'll find a link from where you can download the
                  entire API documentation. Download it, it's worth every single byte it takes on
                  your hard disk drive.

                  kind regards,

                  Jos

                  Comment

                  • Dököll
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 2379

                    #10
                    Originally posted by JosAH
                    It isn't; the API documentation simply documents all the classes in the Java SE
                    core set. It also documents every method and member variable. You should
                    have the API docs open next to your IDE or whatever. There's no need to guess
                    or hack or whatever; when in doubt: read the API docs first.

                    In the Java Articles index you'll find a link from where you can download the
                    entire API documentation. Download it, it's worth every single byte it takes on
                    your hard disk drive.

                    kind regards,

                    Jos
                    Every single byte, eh:-)

                    Will take a closer look...

                    In a bit!

                    Dököll

                    Comment

                    Working...