不知道楼主想要什么结果? class TestClassValue { int x=12; TestClassValue(){ int y = 1; } public static void main(String[] args) { TestClassValue i = new TestClassValue(); System.out.println("x=" + i.x); } }———————————————————————————— class TestClassValue { int x=12; TestClassValue(){ int y = 1; System.out.println("y = "+y); System.out.println("x = "+x ); } public static void main(String[] args) { TestClassValue i = new TestClassValue(); } }
不好意思,楼上的第一个程序少了点东西 class TestClassValue { int x=12; TestClassValue(){ int y = 1; System.out.println("y = "+y); System.out.println("x = "+x ); } public static void main(String[] args) { TestClassValue i = new TestClassValue(); System.out.println("x=" + i.x); //可以这样 } }
System.out.println("x = " + x);
class TestClassValue
{
int x=12;
TestClassValue(){
int y = 1;
}
public static void main(String[] args)
{
TestClassValue i = new TestClassValue();
System.out.println("x=" + i.x);
}
}————————————————————————————
class TestClassValue
{
int x=12;
TestClassValue(){
int y = 1;
System.out.println("y = "+y);
System.out.println("x = "+x );
}
public static void main(String[] args)
{
TestClassValue i = new TestClassValue();
}
}
class TestClassValue
{
int x=12;
TestClassValue(){
int y = 1;
System.out.println("y = "+y);
System.out.println("x = "+x );
}
public static void main(String[] args)
{
TestClassValue i = new TestClassValue();
System.out.println("x=" + i.x); //可以这样
}
}
这句话应该写在你类中定义的某个方法中,在方法之外,一般只允许定义变量的