Method calls in Java

Source: Internet
Author: User

Call "Basics" of methods in Java

First, call the methods in this class

Method one, declared as static by the called method, can be called directly in other methods. The sample code is as follows:

PublicClassHelloword {/** * @param args*/PublicStaticvoidMain (string[] args) {//TODO auto-generated Method Stub String str="helloword!";int a=0;int b=a+1;int result=0;Forint i=0;i<20;i++//add method call Result= Add (A, b); System. out.println (Str+ "" +< Span style= "color: #000000;" > result); A+=i;}} /** * called method, where static method is used to declare * @param x * @param y * @ return */private  static int Add (int x, y) {return x+y;}}   

Method Two, the called method, is not static modification, is not a statically method. Calls need to be made through instantiation of the class. Examples are as follows:

PublicClassHelloword {/** * @param args*/PublicStaticvoidMain (string[] args) {//TODO auto-generated Method Stub String str="helloword!";int a=0;int b=a+1;int result=0;Forint i=0;i<20;i++) {//Add method call//Instantiation of the class Helloword helloword=new Helloword (); // Add method calls by instantiating a class result= helloword. Add (A, b); System. out.println (Str+ "" +< Span style= "color: #000000;" > result); A+=i;}} /** * called method, not statically decorated, not static method. Calls need to be invoked through instantiation of the class * @param x * @param y * @return */private Span style= "color: #0000ff;" >int Add (int x,int Y) { Span style= "color: #0000ff;" >return x+y;}}           

Second, call the method of the external class, through the instantiation of the class. The sample code is as follows:

External classes:

1PublicClassTest {2/**3* Called method add4* @param x5* @param y6* @return7*/8Publicint Add (int x,IntY9{1011Return x+Y12}1314/**15* Called method Sub16 * @param x 17  * @param y< Span style= "color: #008080;" >18  * @return 19 */20 public static int Sub (int x,int Y) 21  {22 23 return x-y; }25}        

Call:

PublicClassHelloword {/***@paramArgs*/PublicStaticvoidMain (string[] args) {//TODO auto-generated Method Stub String str= "helloword!";int a=5;int b=a+1int result=0for (int i=0;i<20;i++ //add method call //new Test (); // Add method calls by instantiating a class result= test. Add (A, b); System.out.println (str+ "" + result); Result=test. Sub (b, 1); System.out.println (str+ "" + result); A+=i;}} } 

Method calls in Java

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.