Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Java what is main(String[]args)

wangshili42

New Coder
Im just a little confused what main(String[]args) is when coding variables. As a newbie, I am also wondering what static void is and if its the "standard" start to every java code. Thanks
 
Hey!

I heard you were confused about the main(String[] args) part in Java and what static void means. Let me break it down for you.

public static void main(String[] args)​

This line is super important in any Java program. Here’s what each part means:

  1. public: This means that any part of the program can access this method. It's necessary because the Java Virtual Machine (JVM) needs to call this method from outside the class.
  2. static: This means the method belongs to the class itself, not to an instance of the class. We need it because the main method is called before any objects are created.
  3. void: This means the method doesn't return anything.
  4. main: This is the name of the method. It's the entry point of any Java application. When you run a Java program, the JVM looks for the main method to start the execution.
  5. String[] args: This is an array of strings. It lets the JVM pass command-line arguments to your program. So if you run your program like java MyClass arg1 arg2, args will contain ["arg1", "arg2"].

Is​

Yep, it is! Every standalone Java application needs this main method as the starting point. Without it, the JVM wouldn’t know where to begin executing your program.

Example to illustrate:​

Here’s a simple Java program to help you understand:

Java:
public class HelloWorld {

    public static void main(String[] args) {
        // Declaring and assigning a variable
        String greeting = "Hello, World!";
       
        // Printing the greeting to the console
        System.out.println(greeting);
       
        // Printing command-line arguments if any
        if (args.length > 0) {
            System.out.println("Command-line arguments:");
            for (String arg : args) {
                System.out.println(arg);
            }
        }
    }
}
  • Class Declaration (public class HelloWorld): This declares a public class named HelloWorld.
  • main Method: Inside the class, we have the main method where the program starts.
  • Variable Declaration (String greeting = "Hello, World!"😉: Here we declare a String variable named greeting and assign it a value.
  • Printing to Console (System.out.println(greeting)😉: This line prints the value of greeting to the console.
When you run this program, it will print "Hello, World!" to the console. If you provide command-line arguments, it will also print those.

So to sum it up:

  • public static void main(String[] args) is the standard starting point for every standalone Java application.
  • static void means the method belongs to the class itself and doesn’t return any value.
  • String[] args allows the program to accept command-line arguments.
Hope this helps clear things up for you!
 
Last edited by a moderator:
Those questions are pretty standard for someone learning Java. So much so that the language has been updated to make it simpler for beginners.
In the latest versions (22/23) of Java this is fully complete and legal program:
Java:
void main() {
    print("Hello World");
}
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom