Results 1 to 18 of 18
  1. #1
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default Classes and main methods?

    Hi there,

    I am new to java and I seem to have hit a frustrating problem of which I do not understand. I have gone on to learn about class members i.e. writing code with several classes of which is fine but I do not know how to then go on to add the main method, for example, here is my code:

    class Zoo {
    public String coolMethod () {
    return "Wow Baby";
    }
    }


    class Moo {
    public void useZoo() {
    Zoo z = new Zoo();
    System.out.println("A Zoo says, " + z.coolMethod());
    }
    }

    This compiles fine accept when I come to run it it complains that "class Zoo does not have a main method", this is a frustrating because when you follow certain excercises in books etc not all of them run, can someone give me a few pointers as to where I place the main method? I have tried to learn from tutorials but it is still difficult to understand

    Hope someone can help

    Thanks

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    well... A main method can be placed almost anywhere. It is the method called at the start of an application. Personally, I just make them call new StartClass(), and make the constructor do what I want, because I find it easier to make constructors than static methods, but that is just me.

    in your case, you could use
    Java Code:
    public static void main(String[] args){
         Moo m = new Moo().useZoo();
         m.useZoo()
    }
    or
    Java Code:
    public static void main(String[] args){
         new Moo().useZoo();
    }
    All programs must have a main method. Whether it does much differs between programs. I have an Inventory program with about 20 classes, and all my main method does is create a class that performs actions for the start of the program:
    Java Code:
    public static void main(String[] args){
         new StartUp();
    }
    BTW, dont forget to use code tags:[code][/code]
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    class Zoo {
        public String coolMethod () {
            return "Wow Baby";
        }	
    }
    
    class Moo {
        public void useZoo() {
            Zoo z = new Zoo();
            System.out.println("A Zoo says, " + z.coolMethod());
        }
    	
        public static void main(String[] args) {
            Moo test = new Moo();
            test.useZoo();
                /*
                 * Or this: (if useZoo() didn't exist)
                 */
            //Zoo z = new Zoo();
            //System.out.println("A Zoo says, " + z.coolMethod());
        }
    }
    The point is that the class you actually name at the command line when you actually run your program has to have a method like the one in the example: declared as "public static void main(String args[])".

    There can be any number of classes which have such a method and any such a class can be "run". The main() method itself is often called an "entry point" for the application.

    As illustrated (in the commented out lines) your useZoo() method itself would do as an entry point if you changed the method's name and signature. More terminology: Moo used in this way is often called a driver class for Zoo. Its purpose is to construct a Zoo instance and call a method. That's about all a nicely written entry point in a driver class will do.

  4. #4
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default

    Brilliant great help guys thanks!! Just one thing tho, I have tried running both examples you gave and I seem to get this error:
    java.lang.NoSuchMethodError: main

    Am I being thick here?

    : )

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It works for me:

    Java Code:
    pbrockway@lind6off:~/Desktop/temp$ cat Whatever.java
    class Zoo {
            public String coolMethod () {
                    return "Wow Baby";
            }
    }
    
    class Moo {
            public void useZoo() {
                    Zoo z = new Zoo();
                    System.out.println("A Zoo says, " + z.coolMethod());
            }
    
            public static void main(String[] args) {
                    Moo test = new Moo();
                    test.useZoo();
                            /*
                             * Or this: (if useZoo() didn't exist)
                             */
                    //Zoo z = new Zoo();
                    //System.out.println("A Zoo says, " + z.coolMethod());
            }
    }
    pbrockway@lind6off:~/Desktop/temp$ javac Whatever.java
    pbrockway@lind6off:~/Desktop/temp$ java Moo
    A Zoo says, Wow Baby
    pbrockway@lind6off:~/Desktop/temp$
    Very often the driver class will actually be public and will go in a file with the same name. Ie in this case you might make both Moo and Zoo public and put them in two separate source files Moo.java and Zoo.java.

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    and... is the main method declaration EXACTLY like the one below? (I don't know if the String array has to be called args, but it always seems to be, so...) Anything else has never worked for me.
    Java Code:
    public static void main(String[] args)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Singing Boyo View Post
    and... is the main method declaration EXACTLY like the one below?
    Java Code:
    public static void main(String[] args)
    In other words, Java is a case sensitive language. main and MAIN are completely two conventions in Java coding. ;)

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Singing Boyo View Post
    Anything else has never worked for me.
    There's always
    Java Code:
    public static void main(String... args) {
    which differs in how it might be called elsewhere in the program.

    But as for the modifiers the JVM leaves no room for doubt about what works and what doesn't: "2.17.1 Virtual Machine Start-up The Java virtual machine starts execution by invoking the method main of some specified class and passing it a single argument, which is an array of strings. This causes the specified class to be loaded (2.17.2), linked (2.17.3) to other types that it uses, and initialized (2.17.4). The method main must be declared public, static, and void."

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Call var-arg method. Same as the array, means any number of arguments in same type(strings in this case) can pass. And also you have to follows some rules as well, to use this.

    And also, in main method this is not use in most cases. I don't know the exact reason for that, but most of the people says its only because of the general practice people have over the years, and still they like to use String[] args.

  10. #10
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default

    Hey guys, really appreciate your help here but I am trying to run this program in netbeans as this is how I have been learning Java and all I am used to. I have checked this code (even copied it from your post) and re-checked it to allign to all your comments but it still gives me the same error - I am baffled?especially when we're getting differences to each other

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Eranga View Post
    [COLOR="DarkGreen"]Call var-arg method. Same as the array, means any number of arguments in same type(strings in this case) can pass.
    Well, no, they're not the same.

    "main(String[] args) {" takes exactly one argument. While "main(String... args)" can take any number (including zero) of arguments. Of course one counts as "any number", and the JVM is quite happy with the var args form as a result. But - to belabour a point - "any number" is not one. They are not the same.

    As I said in my previous post the difference between the two shows up in the way the var args form might be called from elsewhere in the program. (which remains true whether or not tradition, personal inclination or local taboo prevents such a method from actually being called.)

    @OP: In the post where I showed the output I had the code shown in a file called Whatever.java. Try that and use the same commands I did. (Except you will have to replace the "cat" command with "type" if you are on windows.)

    Post the exact and entire output you get.

    To keep the test nice and simple, have Whatever.java be the only the thing in the directory. And don't use a CLASSPATH system variable. The commands I posted assume that the class being run will be found in the current directory and having a CLASSPATH variable can screw that up.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    In fact, to be super sure, post what you get from the following commands copied and pasted from what you see here:

    Java Code:
    dir
    type Whatever.java
    javac -cp . Whatever.java
    java -cp . Moo

  13. #13
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default

    Hi pbrockway2,
    Thanks for your help here but I cannot run programs this way as I have not installed the JDK (I think this is what you need). I am only used to usng netbeans. Whn I try this it just says it doesn't recognise the javac command as I would expect. Thats what I don't understand, am I missing something here in netbeans? I have tried all types of "(String...args)" variations as posted on this forum

    This is tad frustrating for a beginner when he cannot get over the first hurdle: )

  14. #14
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default

    Okay guys I finally got it working on the command prompt line but i still do not understand why I cannot get it working in netbeans even though the code is identical.........

    Pbrockway2, when I try your instructions I get this message:


    error: Class Names, Moo are only accepted if annotation processing is explicitily requested

    Thnx

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by CyberFrog View Post
    Okay guys I finally got it working on the command prompt line but i still do not understand why I cannot get it working in netbeans even though the code is identical.........
    I'm glad you've got the code running. But the important thing is that you understand the role of a static void main() method in how an application "gets going". If that is at all unclear, then ask.

    Sorry - I don't use NB much, and so can't help with that. (and frankly where what must be understood are the concepts like "main method", "classpath" and the rest, ide's will obscure more than "take care of" the details.)

    Quote Originally Posted by CyberFrog View Post
    Pbrockway2, when I try your instructions I get this message:

    error: Class Names, Moo are only accepted if annotation processing is explicitily requested
    Just guessing, but this is the error you get when you attempt the command:

    Java Code:
    javac Moo
    Remember: javac works with files. It turns .java source files into .class files. It wants a file name like "Moo.java".

    java works with classes. It takes a class (nb: a class, not a .class file) and invokes its public static void main() method. It wants a class name like "Moo".
    Last edited by pbrockway2; 05-24-2009 at 11:29 PM.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by pbrockway2 View Post
    Well, no, they're not the same.

    "main(String[] args) {" takes exactly one argument. While "main(String... args)" can take any number (including zero) of arguments. Of course one counts as "any number", and the JVM is quite happy with the var args form as a result. But - to belabour a point - "any number" is not one. They are not the same.
    Well, I apologies here. Actually I use the wrong word here. If I say elements, rather than arguments is much better. It can mess-up to you as well. I refer the String[] as an element as well, and we know that it may have one or many elements.

  17. #17
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default

    Thnaks guys alot more clearer now!!

    Perseverance does pay!!

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. methods, classes, arrays.. oh my!
    By katalyst in forum New To Java
    Replies: 30
    Last Post: 03-30-2009, 12:57 AM
  3. Classes and Methods help
    By border9 in forum New To Java
    Replies: 5
    Last Post: 01-30-2009, 06:51 PM
  4. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •