Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post

    Understanding public static void main

    Code:
    public class HelloWorld
    {
        public static void main(String[] args)
        {
            System.out.println("Hello World");    
        }
    }
    In recently being enrolled in Computer Science (trying to gain experience in java), I don't quite understand the meaning of the code after public class, and how it interacts with displaying the line of code with Hello World. Rather, wouldn't it be more simpler just to display the main string and letting the system display the text itself?
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I don't understand your question.
    System.out.println is required in order to print out text to the screen in Java CLI. You cannot just place "Hello World" on a line and have it displayed on the screen.

    public static void main(String[] argv) is required. This is the entrance method to the application, and at least one main method is required in order to execute the application. When you create a jar, you can specify which class' main method will be the application entrance point. Calling java does not allow you to specify what method is called, so therefore you must call java on an entire class which resolves the main method as the entrance. This is the same in a lot of lanauges; C# and C both have the main methods, and languages like PHP use dummy main() in any userspace code not blocked into a function.

  • #3
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post
    Hello,

    Yes I have more understanding now, especially about how the classes are used and how they play a part in order to execute the function. In JavaScript it would just be displayed in a way likefunction HelloWorld(). So anyways you would need something to fill the hole in, as what the code you stated before would do.
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.


  •  

    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
    •