?
?
1. 在同一个类中——
对于静态方法,其他的静态和非静态方法都可以直接通过“方法名”或者“类名.方法名”调用它。
对于非静态方法,其他的非静态方法可以直接通过类名调用它,但其他的静态方法只有通过对象才能调用它。
2. 在不同类之间——
对于静态方法,通过“类名.方法名”或者对象(不推荐)都能调用。
对于非静态方法,只能通过对象才能调用。
?
?
1. 在同一个包中。
2. 在同一个类中。
3. 被调用的方法为静态方法。
?
方法名;package 源代码;public class Student{ public static void main(String[] args) { m(); } public static void m() { System.out.println("调用成功"); }}编译结果:
调用成功?
?
1.在同一个包中
2.直接通过“类名.方法名()”来实现调用,其中类名是指被调用的方法所处类的名称。
?
类名.方法名();package 源代码;public class Student{ public static void main(String[] args) { Student.m(); } public static void m() { System.out.println("调用成功"); }}编译结果:
调用成功?
?
1. 在同一个包中。
2. 首先定义对象,然后再通过对象来调用方法。
?
类名 对象名=new 类名();对象名.方法名();package 源代码;public class Student{ public static void main(String[] args) { Student student=new Student(); student.m(); } public void m() { System.out.println("调用成功"); }}编译结果:
调用成功