当前位置: 技术问答>java相关
think in java 里的一个例子运行的问题
来源: 互联网 发布时间:2015-06-24
本文导语: class Cup{ Cup(int marker){ System.out.println("Cup("+marker+")"); } void f(int marker){ System.out.println("f("+marker+")"); } } class Cups{ static Cup c1=new Cup(1); static Cup c2=new Cup(2); ...
class Cup{
Cup(int marker){
System.out.println("Cup("+marker+")");
}
void f(int marker){
System.out.println("f("+marker+")");
}
}
class Cups{
static Cup c1=new Cup(1);
static Cup c2=new Cup(2);
Cups(){
System.out.println("Cups()");
}
}
public class ExplicitStatic {
public static void main(String[] args){
System.out.println("Inside main()");
static Cups x=new Cups();
}
}
按书上说的编译器看见static Cups x=new Cups();会先去初始化Cups,至少不会报错,作者也说这不会出错,但是在我机子上编译时却报test/ExplicitStatic.java [32:1] This modifier cannot be used in declaring a local variable
static Cups x=new Cups();
^
1 error
前面StaticInitialization那个例子结构和这个一模一样,可以正常运行
各位能不能帮我解释解释是怎么回事?!
Cup(int marker){
System.out.println("Cup("+marker+")");
}
void f(int marker){
System.out.println("f("+marker+")");
}
}
class Cups{
static Cup c1=new Cup(1);
static Cup c2=new Cup(2);
Cups(){
System.out.println("Cups()");
}
}
public class ExplicitStatic {
public static void main(String[] args){
System.out.println("Inside main()");
static Cups x=new Cups();
}
}
按书上说的编译器看见static Cups x=new Cups();会先去初始化Cups,至少不会报错,作者也说这不会出错,但是在我机子上编译时却报test/ExplicitStatic.java [32:1] This modifier cannot be used in declaring a local variable
static Cups x=new Cups();
^
1 error
前面StaticInitialization那个例子结构和这个一模一样,可以正常运行
各位能不能帮我解释解释是怎么回事?!
|
static Cups x=new Cups();
is in a function body, it is not allowed in Java,
"前面StaticInitialization那个例子结构和这个一模一样"???
check again, the declarations in the book are outside any function bodies
is in a function body, it is not allowed in Java,
"前面StaticInitialization那个例子结构和这个一模一样"???
check again, the declarations in the book are outside any function bodies