[英]How to access or use in a condition the variable from another class
How to access or use in a condition the variable from another class ?? 如何在条件中访问或使用另一个类中的变量? I have a declared variable makol in kstemmer class and i want to use that in stemmer class..
我在kstemmer类中有一个声明的变量makol,我想在干类中使用它。
public class Kstemmer {
private int makol=0;
}
//and this for the stemmer class //这对于词干类
public Stemmer() {
if (makol==0){
System.out.println("avid");
}
}
A variable that is private cannot be used from another class. 私有变量不能在另一个类中使用。 You have to make it public - if they are in the same package you may also leave away both private and public.
您必须将其公开-如果它们在同一个程序包中,则您可能还不愿公开和不公开。
Also, that variable is not static. 同样,该变量不是静态的。 If you want to use it globally, you have to use
static int makol = 0;
如果要全局使用它,则必须使用
static int makol = 0;
and then reference it with Kstemmer.makol
. 然后使用
Kstemmer.makol
引用它。
Alternatively you can instanciate an Object of Kstemmer with Kstemmer someObject = new Kstemmer()
and access the variable with someObject.makol
. 或者,您可以使用
Kstemmer someObject = new Kstemmer()
实例化Kstemmer的对象Kstemmer someObject = new Kstemmer()
并使用someObject.makol
访问变量。
Depending on use case, you would use getters and setters instead of making the variable public. 根据用例,您将使用getter和setter而不是将变量公开。 Non-final variables should almost always be used with getters and setters.
非最终变量几乎应始终与getter和setter一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.