Access philosophy of instance methods and class methods access philosophy of instance variables and class variables

Source: Internet
Author: User

One method in a class is called an instance method and the other is called a class method (static method). The following is a summary of who can access it: i. Class methods can call class methods by class name. class method, Object Name. class Method public class MethodDemo {public static void main (String [] args) {MethodDemo m = new MethodDemo (); m. f (2); MethodDemo. f (3);} public static int f (int x) {int y = 2 * x + 5; m. f (3); return y;} public int m (int x) {int z = 3 * x + 6; return z ;}} method of instance method invocation: object Name. class Method public class MethodDemo {public static void main (String [] args) {MethodDemo m = new MethodDemo (); m. m (3);} public static int f (int x) {int y = 2 * x + 5; return y;} public int m (int x) {int z = 3 * x + 6; return z ;}} 3. instance methods cannot be called in class methods, however, the instance method can call the class method public static int g (int x) {int z = 3 * x + 6; // m (3); return z ;} public int m (int x) {int z = 3 * x + 6; g (3); return z;} can be called in particular: you can call the instance method in the class method, or call the class method public class MethodDemo {public static void main (String [] args) {MethodDemo m = new MethodDemo (); m. m (3);} public static int g (int x) {int z = 3 * x + 6; // class method call the instance method MethodDemo m = new MethodDemo (); m. m (3); return z;} public int m (int x) {int z = 3 * x + 6; // call the class method MethodDemo In the instance method. f (3); f (4); return z ;}} 4. static methods can access variable types static instance methods. Both types of variables can be public class MethodDemo {int shili; static int jintai; public static int g (int x) {int z = 3 * x + 6; // System. out. println (shili); System. out. println (jintai); return z;} public int m (int x) {int z = 3 * x + 6; System. out. println (shili); System. out. println (jintai); return z ;}}

Related Article

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.