Introduction to how static keywords are used in Java three

Source: Internet
Author: User

1.1      Static MethodsMain ()the parsing1.1.1   Static MethodsMain ()the statement composition

in our programming with the Java language, we often use a main () function, which is the entry point of a program and is one of the most important parts of the program. Typically,the main function is written in the following format.

public static void Main (string[] args) {}

we found that the main () function was preceded by static modifier statics, which means that the main function is a static method. So what exactly does this main function statement consist of? Let's examine the composition of the main function statement.

1. Key Words Public , which represents Main The permissions of the function are the largest "Public" .

2. Key Words Static , which represents Main a function is a static method.

3. Key Words void , which represents Main The function does not return a value.

4. Main , which represents the function name and cannot be changed. The change is not the main function, but the custom method.

5. string[] , which represents Main the parameter type of the function is a character array

6. args , which is the parameter name and the only one in the main function that can be modified.

1.1.2   ArgsWhat the hell is that?

Main () main function during the run,the Java Virtual machine passes the parameters to the main () key function, so what parameters does the Java VM Pass? In order to understand this problem, we need to modify main main function, or Car class as an example:

public class Car {public static void main (string[] args) {System.out.println (args);     System.out.println ("args Array length:" +args.length); }}

The results of the operation are as follows:

[ljava.lang.string;@98385c

the args array length is:0

now to analyze the results of the operation, the first line " [Ljava.lang.String] indicates that the object's type is an array of strings, and"@98385c" represents the object's hash value. The second line indicates that the args array has a length of 0. that is, the Java virtual machine passes an array of strings of length 0 to the main function.

1.2      How does a static method invoke a method?

in the previous section we introduced the static method main (), in the static method main () often need to call various methods, including static and non-static methods. For both of these methods, the static method is called by Main () . When the method we need to invoke is a static method, it is very simple to call directly, taking the following code as an example:

public class Car {public static void Stop () {System.out.println ("the car stopped.")         ");     }public staticvoid Main (string[] args) {stop (); }}

The results of the operation are as follows:

The car stopped.

if the main () function calls a non-static method, how should it be used? Let's start by changing the static stop () to a non-static method to see what happens. The code is as follows:

public class Car {public void Stop () {System.out.println ("the car stopped.")         ");     }public staticvoid Main (string[] args) {stop (); }}

errors will be found at compile time.

Error Content: Cannotmake a static reference to the Non-static method Stop () from the type Car

This means that when a method is not a static method, the static method main () cannot be called directly. So how do you call a non-static method? We modify the code in the following manner.

public class Car {public void Stop () {System.out.println ("the car stopped.")         ");         }public staticvoid Main (string[] args) {Newcar (). Stop (); }}

The results of the operation are as follows:

The car stopped.

This shows that the static method main () main ()

Public class car {         voidrun () {                     System.out.println ("The car starts! ");         }         Public static void stop ()  {                    system.out.println ("The car stopped. ");                    newcar (). run ();          }public static void  main (String[] args)  {                    stop ();          }}

The results of the operation are as follows:

The car stopped.

The car starts!

from the running result we can tell that the normal static method and the main () method call the non-static method in the same way.


This article from "Rookie Advanced" blog, reproduced please contact the author!

Introduction to how static keywords are used in Java three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.