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错在哪里
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错在哪里
T2.java:9: 无法从静态上下文中引用非静态 方法 test1()
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();
}
}
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多久?
因为test1()是实例方法,不能在静态方法里面调用,故在原来的test1()方法签名上改为 static void test1()