This article mainly introduces the relevant information about the detailed introduction of the main function in Java. The main() function must appear in the Java program. Here is the usage method. Friends who need it can refer to it
Detailed introduction to the main function in Java
The main function in JAVA is something we are very familiar with. I believe that everyone who has studied the JAVA language can write it skillfully. The entry function of this program is written, but not everyone can easily answer why the main function is written this way and what each keyword in it means. I also encountered this problem while studying. After searching for information on the Internet and adding my own practice, I finally got some insights. I didn't dare to keep them, so I wrote them down to share with everyone.
The general way of writing the main function is as follows:
public static void main(String[] args){…}
The functions of these keywords are explained below:
(1) public keyword , this is easy to understand, declaring the main function as public is to tell other classes that they can access this function.
(2) The static keyword tells the compiler that the main function is a static function. That is to say, the code in the main function is stored in the static storage area, that is, this code already exists after the class is defined. If the main() method does not use the static modifier, there will be no compilation error, but if you try to execute the program, an error will be reported, indicating that the main() method does not exist. Because the class containing main() has not been instantiated (that is, there is no object of this class), its main() method will not exist. Using the static modifier means that the method is static and can be used without instantiation.
(3) The void keyword indicates that the return value of main() is untyped.
(4) Parameter String[] args, this is the focus of this article.
First, program users can pass parameters to a class in the command line state. Look at the following example:
public class ArgsDemo { public static void main(String[] args) { String str = new String(); for (int i = 0; i < args.length; i++) { System.out.println(args[i]); str += args[i]; } System.out.println(str); } }
Use the javac ArgsDemo.java command to generate the ArgsDemo.class file; then use "java ArgsDemo parameter one parameter two parameter three..." The format passes parameters to the ArgsDemo class. This example program will first output the parameters and then output the sum of all parameters. For example, java ArgsDemo a b c will get the following output:
a b c abc
. It should be noted that if the loop condition here is not i
Exception in thread “main” java.lang.ArrayIndexOutOfBoundException:3 at ArgsDemo.main(ArgsDemo.java:5)
Second, you can pass parameters to the class containing main() in another class, as in the following example:
public class A { public static void main(String[] args) { for(int i=0;i <args.length;i++) System.out.println(args[i]); } } public class B { public static void main(String[] args) { c = new A(); String[] b = {"111","222","333"}; c.main(b); } }
First define a class A, in Define a main() function in A, and output the parameter args in this function. Then define a classB, initialize an instance c of A in B, then pass parameters to c, and call the main method of c to print out the passed parameter values. The output result is as follows:
111 222 333
Since the main() function is a static function, it can be used without instantiation, so B can also complete the same function using the following writing method :
public class B { public static void main(String[] args) { //A c = new A(); String[] b = {"111","222","333"}; A.main(b); } }
Summary: The main function of parameter args is to provide a means for program users to interact with the program in the command line state. In addition, it is also feasible to use the main() function directly in other classes and pass parameters. Although this method is not commonly used, it provides us with an option after all.
The above is the detailed content of Sample code sharing about main function in Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment