1、静态方法不可以 直接 调用动态方法或动态变量,但是可以创建动态变量,也可以 直接 调用静态方法和静态变量。
2、对象属于动态的
3、动态的可以调用动态的,也可以调用静态的。
综上:
1)main()方法是静态的,在main()方法中,我们可以直接调用main()方法所在类的静态变量或静态方法。 2)但是,在main()方法中,不能直接调用main()方法所在类(或其他类)的非静态变量或非静态方法。必须先创建一个对象实例后,通过对象名.方法 对象名.变量,通过这个对象才能调用类中的非静态变量或非静态方法。 3)在main方法(或其他方法)内部不能直接定义另一个方法,但是可以在main方法(或其他方法)内部定义一个类,再在类里定义方法。 4)一个方法中的变量是局部变量,只能在该方法内使用,如果想要将要一个方法的局部变量在另一个方法使用,一种方式是将局部变量作为实参,第二种方法是利用方法返回值传递局部变量的值。
Java中静态方法和非静态方法的调用是有区别的。
①静态方法可以直接调用,如下冒泡排序,只需将冒泡方法设为static方法即可直接调用。
public class BubbleSort {
public static void main(String[] args) {
int[] a = {6,5,4,3,2,1,23,14,747};
/* 调用bubbleSort方法:直接调用需将bubbleSort设为静态方法 */
bubbleSort(a);
System.out.println(Arrays.toString(a));
}
public static void bubbleSort(int[] a) {
int temp;
for (int i = a.length - 1; i > 0; i--) {
for(int j = 0; j < i; j++) {
if(a[j] >= a[j+1]) {
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
}
② 非静态方法的调用,需要使用对象来调用。还是冒泡排序示例,如下
public class BubbleSort {
public static void main(String[] args) {
int[] a = {6,5,4,3,2,1,23,14,747};
/* 使用对象调用非静态方法时bubbleSort方法就不需要设为static */
BubbleSort bs = new BubbleSort();
bs.bubbleSort(a);
System.out.println(Arrays.toString(a));
}
public void bubbleSort(int[] a) {
int temp;
for (int i = a.length - 1; i > 0; i--) {
for(int j = 0; j < i; j++) {
if(a[j] >= a[j+1]) {
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
}