main函数调用非static方法

main函数是程序的入口。

如果被调用方法不是静态的,则不能直接调用,要先实例化该类,比如要这样 test t=new test(); 然后才能调用 test.方法名(); 

static类型在程序执行前,系统会为其分配固定的内存,这个区域的成员一旦被分配,就不再改变地址,直到程序结束才释放。如果所有方法都这样做,内存消耗过大,会出现系统崩溃。

public class test {

public static void main(String args[]){
test t=new test();
t.youcanyoufan(2,3);
System.out.println(t.youCanYouFan(2,3));
}

public int youCanYouFan(int a,int b){
return a+b;
}
}

猜你喜欢

转载自www.cnblogs.com/lixiaojing1/p/12597053.html
今日推荐