Results 1 to 13 of 13
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default Public static void main (String args[])

    Java Code:
    class Example1 {
        public static void main(String[] args) {
    
            int temperature = 0;
            if (args.length > 0) {
                try {
                    temperature = Integer.parseInt(args[0]);
                }
                catch(NumberFormatException e) {
                    System.out.println(
                        "Must enter integer as first argument.");
                    return;
                }
            }
            else {
                System.out.println(
                    "Must enter temperature as first argument.");
                return;
            }
    Hello
    1-what is args in String[] args?
    2-why is main static?
    3-why is args.length always zero?

  2. #2
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    1-what is args in String[] args? it is the default name of the variable (the arguments sent to the program via commandline ( or shortcut etc...) (hint, you can change the name))

    2-why is main static? Because otherwise you would need to instantiate it ? TBH I can't answer that well, but basically I think it's just easier for the JVM, the way the devs wrote it...

    3-why is args.length always zero? It's not, it's only zero if you call the program with no arguments.

    try putting a for loop and then calling from commandline:
    program hello bob, my name is joe...

    for ( int i=0; i< args.length; i++ )
    System.out.println(args [i]);

    the output is:
    hello
    bob,
    my
    name
    is
    joe...

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    It's static because otherwise the JVM would have to instantiate an instance of <MainClass>, and therefore would have to know that MainClass had a default constructor...all to simply get the app started up. Whereas with a static main it knows that MainClass.main(args) should exist, and all it has to do is load the class.

  4. #4
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    Thank you very much

    Also I found thease about public static void main(String args[])
    The Main method is the method in which execution to any java program begins.
    A main method declaration looks as follows:

    public static void main(String args[]){
    }


    The method is public because it be accessible to the JVM to begin execution of the program.

    It is Static because it be available for execution without an object instance. you may know that you need an object instance to invoke any method. So you cannot begin execution of a class without its object if the main method was not static.

    It returns only a void because, once the main method execution is over, the program terminates. So there can be no data that can be returned by the Main method

    The last parameter is String args[]. This is used to signify that the user may opt to enter parameters to the java program at command line. We can use both String[] args or String args[]. The Java compiler would accept both forms.
    From: WikiAnswers - Public static void main String arg means
    In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.
    From: Concepts and Code for U: Public static void main (String args[])
    I asked that 3-why is args.length always zero?
    And I entered on command line:
    D:\jdk\bin>java Example1 3 2 5
    and I did not get this message:
    Must enter temperature as first argument.
    Then I changed args[0] to args[1] in my program and wrote:
    D:\jdk\bin>java Example1 3 2 5
    the answer was the same.

    Question: args[0] and args[1] are the same?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    "This is necessary since main( ) is called by the Java interpreter before any objects are made."

    That bit's not quite right since a Class is itself an object (of type Class, in fact). But I get what they're saying.

    As for why you don't get that output, it's because you're not getting a NumberFormatException. And that's because 3 (which is arg[0]) and 2 (which is arg[1]) are both numbers.

  6. #6
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    OK.
    1)with temperature = Integer.parseInt(args[0]);
    -D:\jdk\bin>java Example1 a 3 b
    Result: "Must enter integer as first argument."
    2)with temperature = Integer.parseInt(args[1]);
    -D:\jdk\bin>java Example1 3 a b
    Result: "Must enter integer as first argument."
    3)with temperature = Integer.parseInt(args[2]);
    -D:\jdk\bin>java Example1 3 a b
    Result: "Must enter integer as first argument."

    Thank you.

    main(args) should exist, and all it has to do is load the class.
    Please describe it more.

  7. #7
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Quote Originally Posted by arefeh View Post
    Please describe it more.
    when instantiating an object of a class we use the 'new' keyword, such as Scanner.

    Scanner keyboard = new Scanner(System.in);

    This creates an object of type Scanner in which we can use all it's methods such as next(), nextInt(), hasNext() and so on.

    However with static methods you can simply call on them using the dot notation. For instance this sample code:

    Java Code:
    public class Foo
    {
        public static void bar()
        {
            System.out.println("\'Foobar\' is actually spelled \'Fubar\'.");
        }
        // other non-static methods follow here
        ...
    }
    So to call this method or more importantly while this is still a class and we can still instantiate an object of this class, using the object of the class we cannot call on it's static members.
    To use static methods, we use dot notation like:
    Foo.bar();
    which would print the string: 'Foobar' is actually spelled 'Fubar'.

    So it would be safe to assume that System.out.println is a static method. and print, and printf etc..
    Last edited by Newbie666; 01-27-2010 at 05:22 AM.

  8. #8
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    OK.Thanks a lot.
    But i had a question about args.
    1)args is important for JVM?
    2)args is important for compiler?

    that is, what does the args do in our programs?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    The entry point to your program is defined as a class containing a static method with the signature void main(String[] args). That's it. That's what it looks for.

    The args are there so you can pass data into the app when it starts up, a bit like the various arguments you can give to java (-Xmx, -Xms, -cp, etc). That's all there is to it. It provides a way of the user starting the app to give the app some parameters.

  10. #10
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    As another example a number of PC Games allow you to start in Windowed mode by passing flags to the game such as -w, -win, or -window. Also playing the game with no sound -ns / -nosound or changing the graphics -1600x900, -1400x900 etc (usually by appending flags to the shortcut in program menu)

    Typically the minus symbol is used as a convention to distinguish between settings(flags) and other strings.

    Take this example, this program takes a input flag and output flag and the file names as strings. The input file has all it's lines sorted and the output of the sorted lines in the text is sent to the output file.

    ProgramName -i Input_File.txt -o Output_file.txt
    or
    ProgramName -o Output_File.txt -i Input_File.txt

    so the two switches are -i & -o
    but the variable strings which can be any file names follow the flags.

    The use of flags is not mandatory but it is encouraged, anything could be used as it is you the programmer who has to write the code to deal with what is sent to the program. However it is usually better to stick to standards.
    Last edited by Newbie666; 01-27-2010 at 10:09 AM.

  11. #11
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    Thank you very much about your nice answers.

    1)How do you know these? Do you get these with more practice or more search?
    It provides a way of the user starting the app to give the app some parameters.
    2)I run my program with public static void main() and I got an runtime error:
    Exeption in thread “main” java.lang.NoSuchmethodError:main
    That is, my program does not provide a way for starting up the app.
    OK?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    It follows C in that respect, so that's where I learned about it. Java employs the same concept of a main() with arguments. It's pretty fundamental, and the Sun tutorials no doubt talk about it in the early parts when dealing with the structure of a Java program.

    As for (2), yes...that's not a proper main.

  13. #13
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

Similar Threads

  1. Why can't we write main without String args[]
    By shailender in forum New To Java
    Replies: 6
    Last Post: 06-17-2013, 09:28 AM
  2. Replies: 9
    Last Post: 11-22-2008, 11:03 PM
  3. Replies: 5
    Last Post: 10-06-2008, 12:11 AM
  4. Replies: 5
    Last Post: 01-24-2008, 11:53 PM

Posting Permissions

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