Java main method

Melissa 109 Published: 10/09/2024

Java main method

Here's a comprehensive explanation of the main method in Java, which is the entry point for any Java program.

The main method is declared as follows:

public static void main(String[] args) {

// code goes here

}

Let's break down what each part means:

public: This keyword indicates that the main method can be accessed from anywhere in the program. static: This keyword means that the main method is a class-level method, meaning it doesn't require an instance of the class to be called. It's also the entry point for any Java program. void: This keyword indicates that the main method doesn't return any value. main: This is the name of the method. It must match exactly with the string "main" (case-sensitive). (String[] args): This is the parameter list for the main method. The args variable is an array of strings that represents the command-line arguments passed to the program.

When you run a Java program, the JVM (Java Virtual Machine) calls the main method and passes it any command-line arguments that were provided. For example, if you run the following command:

java MyProgram hello world

The JVM would pass two strings, "hello" and "world", to the main method.

Here's a simple example of how you might use the main method:

public class MyProgram {

public static void main(String[] args) {

for (String arg : args) {

System.out.println("Argument: " + arg);

}

}

}

In this example, we're printing out each command-line argument that was passed to the program. If you run the following command:

java MyProgram hello world

You would see the output:

Argument: hello

Argument: world

The main method is where you'd typically put your program's initialization code, such as setting up GUI components or reading data from a file. It's also where you might call other methods that are specific to your program.

One important thing to note about the main method is that it must be declared exactly once in each Java program. If you try to declare multiple main methods with different signatures (e.g., int main(String[] args) instead of void main(String[] args)), the compiler will give you an error.

That's a basic overview of the main method in Java!

What are main methods java example w3schools

I'll respond in English for now .

W3Schools provides an excellent Java tutorial section with various examples and exercises to help beginners learn the language. Here, we will focus on the main methods in Java, along with some examples from W3Schools:

What are Main Methods?

In Java, a main method is the entry point of any Java program. It's where the program starts execution when you run it. The main method is declared as public static void main(String[] args) in every Java program.

Example 1: Simple Java Program

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

}

}

This example demonstrates a simple Java program that prints "Hello, World!" to the console. The main method is where the program starts execution.

Example 2: Command-Line Arguments

public class ArgsExample {

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

System.out.println("Argument " + i + ": " + args[i]);

}

}

}

In this example, you can pass command-line arguments to the program. The main method iterates through the array of strings (args) and prints each argument.

Example 3: User Input

import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your name: ");

String name = scanner.nextLine();

System.out.println("Hello, " + name + "!");

}

}

This example demonstrates how to get user input using the Scanner class. The program prompts the user to enter their name and then prints a greeting message with the entered name.

Example 4: Method Call

public class MethodCall {

public static void main(String[] args) {

greet("John");

greet("Jane");

}

public static void greet(String name) {

System.out.println("Hello, " + name + "!");

}

}

In this example, the main method calls another method (greet) with different arguments. The greet method prints a greeting message with the passed name.

These examples demonstrate the basic usage of the main method in Java. W3Schools provides many more tutorials and exercises to help you learn Java programming.