Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of main method in Java

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what is the use of the main method in Java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. In-depth understanding of main method

Explain the form of the main method: public static void main (String [] args) {}

The 1..main method is called by the virtual machine

The 2.java virtual machine needs to call the main () method of the class, so the access to this method must be public

The 3.java virtual machine does not have to create an object when executing the main () method, so the method must be static

4. This method receives an array parameter of type String, which holds the parameters passed to the running class when the java command is executed.

Program parameter 1, parameter 2, parameter 3 executed by 5.java

Public class Hello {public static void main (String [] args) {/ / how is args passed in / / traversing shows for (int I = 0; I < args.length; iTunes +) {System.out.println ("th" + (I + 1) + "parameter =" + args [I]);}

Special hint:

1. In the main () method, you can directly call the static method or static property of the class where the main method resides.

two。 However, non-static members of the class cannot be accessed directly, and an instance object of the class must be created before the non-static members of the class can be accessed through this object.

Public class Main01 {/ / static variable private static String name = "charming"; / / non-static variable private int N1 = 1000; / / static method public static void hi () {System.out.println ("Main01's hi method");} / / non-static method public void cry () {System.out.println ("Main01's cry method") } public static void main (String [] args) {/ / you can use name / / 1 directly. The static method main can access the static member System.out.println ("name=" + name) of this class; hi (); / / 2. The static method main cannot access the non-static member of this class / / System.out.println ("N1 =" + N1); / / cry (); / / 3. Static method main to access non-static members of this class, you need to create an object first, and then call Main01 main01 = new Main01 (); System.out.println (main01.n1); main01.cry ();}}

two。 How to pass parameters under Idea

Case Main02.java

Public class Main02 {public static void main (String [] args) {for (int I = 0; I < args.length; I +) {System.out.println ("th" + (I + 1) + "parameter =" + args [I]);}

Thank you for reading this article carefully. I hope the article "what is the use of main in Java" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report