日期:2014-05-20  浏览次数:20713 次

请大家帮我看看这个简单的java程序
public class T2{  
Long a = new Long(14);
void test1(){
long b = a.longValue();
System.out.println("b=== "+ b); 
}

public void main(String[] args){
test1();
}
}

D:\javatest>java T2
Exception in thread "main" java.lang.NoSuchMethodError: main

错在哪里

------解决方案--------------------
main是静态方法,要把test1也改成静态才能被main调用。

private static void test1(){
------解决方案--------------------
main函数里没有实例化T2类
------解决方案--------------------
你的主函数应该是静态的哈,
------解决方案--------------------
1楼正解,要写完整
两种写法
public class aaa{
 Long a = new Long(14);
 void test1(){
long b = a.longValue();
System.out.println("b=== "+ b);


public void main(String[] args){ 
aaa test = new aaa();
test.test1();

}
第2种
public class aaa{
 static Long a = new Long(14);
 static void test1(){
long b = a.longValue();
System.out.println("b=== "+ b);


public static void main(String[] args){ 
test1();

}
------解决方案--------------------
public class aaa{
 Long a = new Long(14);
 void test1(){
long b = a.longValue();
System.out.println("b=== "+ b);
}

public void main(String[] args){
aaa test = new aaa();
test.test1();
}
}
第2种
public class aaa{
 static Long a = new Long(14);
 static void test1(){
long b = a.longValue();
System.out.println("b=== "+ b);
}

public static void main(String[] args){
test1();
}
}
就这两种 好好看看 楼主学JAVA多久?
------解决方案--------------------
main要定义为静态方法,修改为public static void main(String[] args)
因为test1()是实例方法,不能在静态方法里面调用,故在原来的test1()方法签名上改为 static void test1()
------解决方案--------------------
静态类,实现的方法必须是静态。

如果该方法不是静态,那么得实现实例化该类,如,T2 test = new T2();