[英]Java instance variable declare and Initialize in two statements
Hi I'm having problem with initialization in java, following code give me compile error called : expected instanceInt = 100; 嗨,我在使用Java进行初始化时遇到问题,以下代码给了我一个编译错误,叫做:Expected instanceInt = 100; but already I have declared it.
但我已经宣布了 If these things relate with stack and heap stuff please explain with simple terms and I'm newbie to java and I have no advanced knowledge on those area
如果这些东西与堆栈和堆的东西有关,请用简单的术语进行解释,我是Java的新手,并且我对这些领域不了解。
public class Init {
int instanceInt;
instanceInt = 100;
public static void main(String[] args) {
int localInt;
u = 9000;
}
}
You can't use statements in the middle of your class. 您不能在课程中间使用语句。 It have to be either in a block or in the same line as your declaration.
它必须与声明位于同一块或同一行中。
The usual ways to do what you want are those : 做您想要的事情的通常方法是:
The initialization during the declaration 声明期间的初始化
public class MyClass{ private int i = 0; }
Usually it's a good idea if you want to define the default value for your field. 通常,如果要为字段定义默认值,则是一个好主意。
The initialization in the constructor block 构造函数块中的初始化
public class MyClass{ private int i; public MyClass(){ this.i = 0; } }
This block can be used if you want to have some logic (if/loops) during the initialization of your field. 如果您希望在字段初始化期间具有一些逻辑(如果/循环),则可以使用此块。 The problem with it is that either your constructors will call one each other, or they'll all have basically the same content.
问题在于,要么您的构造函数将彼此调用,要么它们将具有基本相同的内容。
In your case I think this is the best way to go. 以您的情况,我认为这是最好的方法。
The initialization in a method block 方法块中的初始化
public class MyClass{ private int i; public void setI(int i){ this.i = i; } }
It's not really an initialization but you can set your value whenever you want. 这并不是真正的初始化,但是您可以随时设置值。
The initialization in an instance initializer block 实例初始化程序块中的初始化
public class MyClass{ private int i; { i = 0; } }
This way is used when the constructor isn't enough (see comments on the constructor block) but usually developers tend to avoid this form. 当构造函数不够用时使用这种方法(请参阅构造函数块的注释),但通常开发人员倾向于避免这种形式。
Resources : 资源:
On the same topic : 在同一主题上:
Bonus : 奖励:
What does this code ? 这是什么代码?
public class MyClass {
public MyClass() {
System.out.println("1 - Constructor with no parameters");
}
{
System.out.println("2 - Initializer block");
}
public MyClass(int i) {
this();
System.out.println("3 - Constructor with parameters");
}
static {
System.out.println("4 - Static initalizer block");
}
public static void main(String... args) {
System.out.println("5 - Main method");
new MyClass(0);
}
}
Put it in an initializer block. 将其放在初始化程序块中。
public class Init {
int instanceInt;
{
instanceInt = 100;
}
public static void main(String[] args) {
int localInt;
int u = 9000;
}
}
Or simply initialize while declaring it: 或者只是在声明时初始化:
public class Init {
int instanceInt = 100;
public static void main(String[] args) {
int localInt;
int u = 9000;
}
}
References: 参考文献:
You can not have a separate statement to assign values to class members in the declaration area. 您不能在声明区域中使用单独的语句将值分配给类成员。 You have to do this from a function, typically a class method.
您必须从函数(通常是类方法)执行此操作。
For your case, consider doing the assignment as soon as you declare. 对于您的情况,请考虑在声明后立即进行分配。
You need to do : 您需要做:
int instanceInt = 100;
If it was static
you could initialize in a static
block. 如果它是
static
,则可以在static
块中初始化。
According to the Java Language Specification : 根据Java语言规范 :
A class body may contain declarations of members of the class, that is, fields (§8.3), classes (§8.5), interfaces (§8.5) and methods (§8.4).
类主体可以包含类成员的声明,即字段(第8.3节),类(第8.5节),接口(第8.5节)和方法(第8.4节)。 A class body may also contain instance initializers (§8.6), static initializers (§8.7), and declarations of constructors (§8.8) for the class.
类主体还可以包含实例初始化器(第8.6节),静态初始化器(第8.7节)和该类的构造函数的声明(第8.8节)。
However, the statement 但是,声明
instanceInt = 100;
is none of those things, therefore it is not allowed as part of a class body. 这些都不是,因此不允许作为类主体的一部分。 What you need to do is this:
您需要做的是:
int instanceInt = 100;
This is allowed because it is a field declaration that includes a variable initializer . 这是允许的,因为它是包含变量初始值设定项的字段声明 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.