Introduction to the Basics of Java’s ‘public static void main String[] args’
The line public static void main String[] args is a fundamental part of Java programs, serving as the entry point for the program execution. This article delves into the components and significance of this line for anyone new to Java programming or looking to refresh their understanding of the basics.
Breaking Down the Components
The `public static void main String[] args` line is structured as follows:
1. public
This keyword specifies that the method is accessible from anywhere. In Java, the main method must be declared as public so that the Java Virtual Machine (JVM) can call it from outside the class.
2. static
The static keyword indicates that the method belongs to the class itself rather than to any specific instance of the class. Consequently, you can invoke the main method without creating an object of the class.
3. void
The void keyword indicates that the main method does not return any value. Since it is the entry point of the program, there is no need for it to return a result.
4. main
This is the name of the method. The JVM looks for this specific method name when starting the execution of a Java program. It is a requirement as it serves as the entry point for the program.
5. String[] args
This part of the line specifies that the main method accepts a single parameter: an array of String objects. This array holds any command-line arguments passed to the program when it is executed. For example, if you run a Java program with java MyClass arg1 arg2, the args array will contain ["arg1", "arg2"].
Why is it Written?
The main method is a requirement in Java applications because it serves as the entry point for program execution. When you run a Java application, the JVM looks for the main method to start executing the code. Without this method, the program would not know where to begin.
Example: A Simple Java Program
Below is a simple example of a Java program that uses the main method:
public class HelloWorld { public static void main(String[] args) { Console.WriteLine("Hello, World!"); }}
When you run the HelloWorld class, it executes the main method, which then prints "Hello, World!" to the console.
Static in Java
Access Modifiers and Static Method
In Java, the public keyword is an access modifier that allows the main method to be accessible from anywhere. In the following example:
1. public int Add(int a, int b)
This method is performing some action (adding two numbers) and the return type of the method is int, giving back an integer value.
2. public static void Say()
This method does not return any value. It is marked as static, indicating that the method belongs to the class itself rather than to any specific instance of the class. The static method can be called without creating an instance of the class.
Main Method in Java
The Main method is adopted as the specific name for the key to starting your code. The runtime is designed to look for a method called Main that has the following properties:
1. Static, void, no return
This indicates that the method does not return any value and belongs to a class. The method name is Main, just like any other method name. This is how the runtime knows to start executing code from this point.
Historical Context and Usage
Main methods are used in many modern programming languages, including C, C , C#, Java, Kotlin, and Scala. However, not all languages have them, such as Ruby, which is a scripting language where scripts are often executed linearly. Ruby scripts are executed from the first line to the last without requiring a specific Main method because this is part of Ruby’s design.
Command-line Arguments in Java
The String[] args in the Main method represents the command-line arguments that are passed to a program when it’s run from the command line. Here’s an example:
using System;class Program{ static void Main(string[] args) { Console.WriteLine("You provided the following arguments:"); for (int i 0; i args.Length; i ) { Console.WriteLine(args[i]); } }}
Running dotnet run hello world will output:
You provided the following arguments: Argument 1: hello Argument 2: world
Should you always have to write String[] args in the Main method? In C#, the Main method can either take a String[] args parameter or it can take no parameters at all. If you’re not using command-line arguments in your program, you can have a parameterless Main method. This is valid and commonly used in simple programs where you don’t need to use command-line arguments.
Exercises
Below are some coding exercises to help you test and practice your understanding:
1. Simple Example:
public class Program{ public static void Main(string[] args) { Console.WriteLine(args[0]); }}
2. Method with Addition:
public class Program{ private static int Add(int a, int b) { return a b; } public static void Main() { Console.WriteLine(Add(3, 4)); }}
3. Conditional Check:
public class Program{ public static int Main(string[] args) { if (args.Length 0) { Console.WriteLine(args[0]); return 0; } else { return 1; } }}
4. Calling Static Methods:
public class Program{ private static void Greet() { Console.WriteLine("Hello, World!"); } public static void Main(string[] args) { Greet(); }}
5. Simple Loop:
public class Program{ public static void Main(string[] args) { for (int i 0; i 5; i ) { Console.WriteLine(i); } }}
Good luck!