JAVA Main方法中调用非静态方法

在JAVA中静态方法中只能调用其他,静态方法。main方法都是静态方法,如果想调用其它的方法,要么只能是其它的静态方法。还有一种方法就是将当前类实例化在调用它的非静态方法

public class text1{ 
public static void main(String [] args){ 
int a = 12; 
int b = 23; 
text1 aa = new text1(); 
aa.add(a,b); 

public void add(int a,int b){ 
System.out.println("计算的结果是:"+(a+b)); 

}
 
main方法里面调用非静态方法时,需要new一个对象,因为静态方法,静态类属于模板,非静态类和方法属于对象。

猜你喜欢

转载自www.cnblogs.com/zhangruifeng/p/9326500.html