这个java代码中的错误是什么 [英] what is error in this java code

查看:104
本文介绍了这个java代码中的错误是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:

my code is this:

class first{
	int x,y;
	x = y = 0;
}
class second{
	public static void main(String args[]){
		first x = new first();
		System.out.println(x.x);
		System.out.println(x.y);
	}
}



错误:

< identifier>除外

x = y = 0;

^

当我写这样的代码时:


Error:
<identifier> excepted
x = y = 0;
^
When I write this code like this:

class first{
	int x=0;
	int y=0;
}
class second{
	public static void main(String args[]){
		first x = new first();
		System.out.println(x.x);
		System.out.println(x.y);
	}
}



没有错误,请解释我为什么以前的代码无效


there is no error, please explain me why the previous code not work

推荐答案

beacuse

beacuse
int x = 0;



变量声明和初始化


is variable declaration and initialization while

x = 0;

赋值,不允许在那里。


嗯。

如果不将代码放在函数体内,就不能将代码放入类中。将初始化器放在类构造函数中,你会没事的。
Um.
You can't just put code into a class without it being inside a function body. Put the initializer in the class constructor and you'll be fine.


这篇关于这个java代码中的错误是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆