[英]How do I refer to a variable from a method in the same class
I surprisingly find this confusing. 我惊讶地发现这令人困惑。 I must be missing something.
我肯定错过了什么。
So I have this simple syntax 所以我有这个简单的语法
public class OMG{
public static void main(String args[]){
int hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
hi+=1;
}
}
Obviously this cause an error, since hi is a local variable. 显然这会导致错误,因为hi是局部变量。
Judging from my experience from python I added this
根据我从python的经验来看,我添加了
this
public class OMG{
public static void main(String args[]){
int hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
this.hi+=1;
}
}
Which adds extra error when non-static variable cannot be accessed from a static method. 当无法从静态方法访问非静态变量时,会增加额外的错误。
I added static
to hi 我给
static
加了static
public class OMG{
public static void main(String args[]){
static int hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
this.hi+=1;
}
}
The compiler scolds me for a illegal expression. 编译器骂我非法表达。 I substitute the static with
private
(which some SO answers, recommend) but same error. 我用
private
代替静态(有些SO答案,推荐)但是同样的错误。
Where's my mistake? 哪里是我的错? Is there any way I can solve this, without making global class?
有没有什么方法可以解决这个问题,而无需进行全球课程?
You cannot declare static
variables inside a method because static
modifier means that a method or field belongs to the class. 您不能在方法中声明
static
变量,因为static
修饰符意味着方法或字段属于该类。
The easiest solution to this problem would be to declare the variable as static
class variable. 解决此问题的最简单方法是将变量声明为
static
类变量。 Using this approach, you also need to remove this
from this.hi
in lestDoIt
method. 使用这种方法,您还需要在
lestDoIt
方法中从this.hi
中删除this
。 The code would be like this: 代码如下:
public class OMG {
static int hi=2;
public static void main(String args[]) {
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt() {
hi+=1;
}
}
Another solution may be using a non static
variable hi
. 另一种解决方案可以是使用非
static
变量hi
。 This would need you to also remove the static
modifier to the letsDoIt
method to access to hi
field, because static
methods cannot access to instance fields because, as explained above, static
means that a method or field belongs to the class and not to a specific object instance of the class. 这还需要你去除
letsDoIt
方法的static
修饰符来访问hi
字段,因为static
方法无法访问实例字段,因为如上所述, static
意味着方法或字段属于类而不属于特定该类的对象实例。
The solution would be: 解决方案是:
public class OMG {
int hi=2;
public static void main(String args[]) {
//note that we have to create a new instance of OMG
//because, again, static methods cannot access to non-static methods/fields
OMG omg = new OMG();
omg.letsDoIt();
System.out.println(omg.hi);
}
public void letsDoIt() {
this.hi+=1;
}
}
More info: 更多信息:
Static variables are variables of the class, not its instances. 静态变量是类的变量,而不是其实例。 You can't have a static variable inside a method.
您不能在方法中包含静态变量。
To fix this error, move hi
outside the main method (keeping it static). 要修正这个错误,将
hi
的主要方法外(保持它的静态)。 Also get rid of the this
in letsDoIt()
. 也摆脱了
this
在letsDoIt()
public class OMG {
static int hi=2;
public static void main(String args[]){
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt() {
hi+=1;
}
}
There are two elements causing your issue. 导致您出现问题的原因有两个。
hi
must be referenced within a shared context between your main
method and your letsDoIt
method hi
必须在main
方法和letsDoIt
方法之间的共享上下文中引用 main
method is static
, and so your letsDoIt
, the will only have visibility on static
fields (unless passed an instance as argument, that is) main
方法是static
,所以你的letsDoIt
,只会对static
字段有可见性(除非作为参数传递实例,即) Hence: 因此:
Declare hi
as a static
field of OMG
: 将
hi
声明为OMG
的static
字段:
public class OMG { static int hi; ...
Remove the local variable declaration in your main
method. 删除
main
方法中的局部变量声明。
Reference it with OMG.hi
or just hi
within a static
context, not with this.hi
as this
implies an instance of Main
, which is not visible from a static
context 参照它
OMG.hi
或只是hi
一内static
情况下, 不与this.hi
因为this
意味着一个实例Main
,这是不可见的static
背景
You can not do "this.hi+=1" in a static context and in order to access the hi variable from "letsDoIt()" you have to declare it as a class variable like I did in the code below: 您不能在静态上下文中执行“this.hi + = 1”,并且为了从“letsDoIt()”访问hi变量,您必须将其声明为类变量,就像我在下面的代码中所做的那样:
public class OMG{
public static int hi;
public static void main(String args[]){
hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
hi+=1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.