Java中main函数只能调用同类中的静态方法?
如果想调用本类中的非静态方法可以这么来写:
1 public class TT{ 2 public static void main(String[] args){ 3 TT t = new TT(); 4 t.fun(); 5 } 6 void fun(){ 7 System.out.println("hehe"); 8 } 9 }
为什么InPut方法少了static就不行?
1 public class First { 2 public static void main(String[] args) { 3 4 InPut("shuzu"); 5 } 6 7 public static void InPut(String t){ 8 System.out.println(t); 9 } 10 }
对于一般的非static成员变量或方法,需要有一个对象的实例才能调用,所以要先生成对象的实例,他们才会实际的分配内存空间。
而对于static的对象或方法,在程序载入时便已经分配了内存空间,他只和特定的类想关联,无需实例化。
posted on 2015-06-01 09:24 Dominic_MA 阅读(738) 评论(0) 收藏 举报