Results 1 to 19 of 19

Thread: main method

  1. #1
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Post main method

    Hi,

    Can you explain to me how the main method works.
    Why each keyword is needed for the main method to function.
    Is the args parameter needed with this basic output command?

    Java Code:
    public static void main(String args[]) {
        System.out.println("Hello World");
    }
    Sorry if this is too basic for you, Im very new to Java.
    As simple as possible please. Thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    When you run your program you actually start the JVM which then calls YourProgram.main(args). This is why the method needs to be static and accept a String array as a parameter. Otherwise the JVM cannot find it.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by 007 View Post
    Hi,
    Can you explain to me how the main method works.
    I am not an expert so my explanation is base on what I understand and might not as deep as how others understand it. main method is the first thing that JVM search for and where your program begin. Every application (Applet is excluded) should have main method, as I said, this is the first thing that JVM searches.


    Quote Originally Posted by 007 View Post
    Is the args parameter needed with this basic output command?
    String[] args is not actually needed, do not get me wrong, what I mean is it is optional. It depends on the program if you will require it or not.

    Here are two example, one requires your input before program executes. The other one executes then ask for your input
    in your command line you will type something like
    java className mine0926
    Java Code:
    public static void main(String[] args)
    {
        String yourName = args[0];  //<-- args[0] is your input which is mine0926
        System.out.println("Your name is : " + yourName);
    }
    in your command line you will type something like
    java className
    Java Code:
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is your name?");
        String yourName = scanner.readLine();
        System.out.println("Your name is : " + yourName);
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by mine0926 View Post
    String[] args is not actually needed, do not get me wrong, what I mean is it is optional. It depends on the program if you will require it or not.
    Just to clairfy: The String array parameter MUST be present. What is optional is wether your program uses it or not.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    That is what I am trying to explain, thanks for clarifying it, :)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In case you don't know all the modifiers:
    Public is access control, it allows the method to be accessed from anywhere(you can also have private, protected, and default(package access) play around with different access modifiers for main and see how the jvm reacts)

    Static is the type of method, a static method can be accessed without first creating an instance of the class. You can reference static methods with ClassName.MethodName, the methods on the math class are mostly static(I say mostly because I'm not certain that none are static)

    Void is the return type, a method that has a void return type simply does something and does not return anything.

  7. #7
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Default

    main() is where a program always start. I tried making it private, protected, and package but those three didnt work
    so therefore main() needs to be public because the JVM calls the main method outside of its class.

    It need to be static so that the main method can be called before making any objects of the class.

    It is void because it didnt return anything but is it possible to change void into something else that would return something?
    Java Code:
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is your name?");
        String yourName = scanner.readLine();
        System.out.println("Your name is : " + yourName);
    }
    Thanks for the examples. I tried running this but it doesn't seem to work. But I can see how the code work: when the user types in there name, it
    returns the typed name.

    So it is up to me if I want to use the args parameter or not. But are there any cases when it is necessary to use the parameter? It does seem a bit inconvenient.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by 007 View Post
    It is void because it didnt return anything but is it possible to change void into something else that would return something?
    Try it and see.

    I tried running this but it doesn't seem to work.
    Explain what "doesn't work" means. There could be a gazillion different causes.

    So it is up to me if I want to use the args parameter or not. But are there any cases when it is necessary to use the parameter? It does seem a bit inconvenient.
    No. Since the programmer has to write the main method what happens in that method is totally up to the programmer. You get to choose. The only thing that you are forced to do is write a main method with that exact signature.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Note that Applets don't require a p.s.v.main(...) method, nor do MIDlets (Java ME). The p.s.v.main(...) method is the entry point for applications.

    db

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by 007 View Post
    I tried running this but it doesn't seem to work.
    It will not work because it is not in a class. We said before that an application must have main method, in additional main method should be place in a class.
    So jvm will call YourClassName.main() as a beginning of your application.

    The two example that I show do same thing, the only difference I how the programmer ask the user to input user's name.

    Quote Originally Posted by 007 View Post
    But I can see how the code work: when the user types in there name, it
    returns the typed name.
    I just want to clear up that main method did not return the input but rather print what has been inputted. The word "return" means something in programming language especially in java.

  11. #11
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Default

    I tried putting an int instead of void and it gave me an error even though I returned an int.

    Java Code:
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is your name?");
        String yourName = scanner.readLine();
        System.out.println("Your name is : " + yourName);
    }
    Sorry I was talking about the code above. I tried running it and it gave an error "The method readLine() is undefined for the type Scanner"
    Maybe readLine() is not a method in Scanner class.

    I can see how important the main method is. Thanks for giving me a clear idea about how the main method works. If there are anything I need to know more about the main () method, it would be nice if you could tell me. But I guess this info will suffice for now.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

  13. #13
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Sorry for not checking and testing it, change readLine() to nextLine()

  14. #14
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Default

    I just want to clear up that main method did not return the input but rather print what has been inputted. The word "return" means something in programming language especially in java.
    In other words it prints the input after the user types a name, not return. return is when you return a value like an int, double, boolean, etc. I'll definitely put it in mind. Thanks.

  15. #15
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Yes. main method should always be written the same way the only thing you can change there, if I am not mistaken, is the name of string which is "args". But the standard way of writing it, mosty programmers write it so anyone who read it will automatically know that it is the main method, is public static void main(String[] args).

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    the only thing you can change there, if I am not mistaken, is the name of string which is "args"
    The parameter name, which is of type String[], not String. But yes, it's entirely legal to change that name e.g.
    Java Code:
    public static void main(String[] arrrrrgghhhh) {
    //
    }
    db

  17. #17
    srid12 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    pub;ic implies accesed any where inside package
    static implies it can be called only once
    void implies does not return any thing
    string[] args : this implies that u can pass array of strings to main method as input
    please see my blog Moderator edit: link removed
    Last edited by DarrylBurke; 06-25-2011 at 06:13 PM. Reason: Removed link

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,880
    Rep Power
    25

    Default

    static implies it can be called only once
    Not true. A static method can be called repeatedly as many times as you want.
    void implies does not return any thing
    "implies" is redundant here

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Static doesn't imply that a method can only be called once. Not only is it possible, but very possible to create a recursive static method. You can also call main inside main(although this will cause infinite recursion, it is possible)

    Java Code:
    public class X{
      public static void main(String[] args){
        X.main(args);
      }
    }
    As far as I know this works, you can test it and verify.

Similar Threads

  1. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:30 AM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 12:03 PM
  3. Main Method Help
    By thomas6 in forum New To Java
    Replies: 0
    Last Post: 03-12-2010, 02:20 AM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 06:30 PM
  5. Where do I put main method?
    By jerryrice80 in forum New To Java
    Replies: 4
    Last Post: 09-16-2009, 09:23 PM

Tags for this Thread

Posting Permissions

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