In Java, one method is used to call another method. In java, another method is called.

Source: Internet
Author: User

In Java, one method is used to call another method. In java, another method is called.

In the same class:

For static methods, other static or non-static methods can be called directly.

For non-static methods, other non-static methods can be called directly. However, other static methods can be called only through objects.

Static methods cannot be overwritten by non-static methods.

Public class Test2 {public static void s1 () {System. out. println ("s1");} public void say1 () {System. out. println ("say1");} public void say () {s1 (); say1 (); // call say1 method} public static void main (String [] args) {s1 (); Test2 t = new Test2 (); t. say ();}}

Jpg

For different classes, whether the called method is non-static or static, if the called method is:

Static Method, you can call both the class name and object (but it is not recommended to use the object method because it is a non-static call method)

A non-static method can be called only through an object.

Public class CallTest2 {public static void s () {System. out. println ("s1");} public void say () {Test2 t2 = new Test2 (); // call the method t2.say () in Test; t2.s1 (); test2.s1 ();} public static void main (String [] args) {CallTest2 t = new CallTest2 (); t. say ();}}

In the above Java example, calling another method is to share all the content with you. I hope to give you a reference, and I hope you can provide more support to the customer's house.

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.