Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Java method

Shulou Source: shulou.com Published: 2022-06-01 11:34:17 09月11日 Update

This article introduces the relevant knowledge of "how to use the Java method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When we learn operators, we need to create a new class and main method for each operator separately, and we will find that writing code in this way is very cumbersome and repetitive.

Whether you can avoid these duplicate codes, you need to use methods to implement them.

01

Definition of method

The method is to extract a function and define the code in a single curly braces to form a separate function.

When we need this function, we can call it. This not only realizes the reusability of the code, but also solves the phenomenon of code redundancy.

Define the format:

The modifier returns the value type method name (parameter list) {code. Return;}

For example:

Public int getNum (int A1) {int num=a1*2; return num;}

02

Method invocation

After the method is defined, the method will not run on its own, but must be called before it can be executed. We can call our own defined method in the main method main. In the main method, you can simply write the name of the method to be called.

Public static void main (String [] args) {/ / calls the defined method ();} / / defines the method, which is called by the main method public static void method () {System.out.println ("self-defined method that needs to be called and run by main");}

Note:

Methods must be defined in one class. Methods outside methods cannot be defined in another method. "how to use the Java method" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Method code function content time more knowledge operator learning operation running practical tedious successful next item method of use redundancy parameter name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS Shulou Information Xiaomi MariaDB