[英]StackOverflowError when trying to call a method which calls another method from another class
I think the method is calling itself or something. 我认为该方法正在调用自身或其他内容。
Here's my code: 这是我的代码:
public boolean isRaak(int rij, int kolom)
{
boolean raak = isRaak(rij, kolom); //this "isRaak" should refer to a method in another class, not sure how to do this...
return raak;
}
Two ways 两种方式
If that is a instance method, You need to create instance
of that and call it. 如果这是一个实例方法,则需要创建该
instance
并调用它。
public boolean isRaak(int rij, int kolom)
{
AnotherClass an =new AnotherClass();
boolean raak = an.isRaak(rij, kolom);
return raak;
}
If that is a static method 如果那是静态方法
public boolean isRaak(int rij, int kolom)
{
boolean raak = AnotherClass.isRaak(rij, kolom);
return raak;
}
But your method seems like an Utility
method to me,So go for static method,if so. 但是对于我来说,您的方法似乎是一种
Utility
方法,因此,请使用静态方法。
Before proceeding further, Prefer to read: 在继续进行之前,请先阅读以下内容:
The error is pretty evident. 该错误非常明显。 Call the method by prefixing the classname if the method is static, else call the method with the object of the class.
如果方法是静态的,则通过在类名前添加前缀来调用该方法,否则使用该类的对象来调用该方法。
I can think of 3 ways to accomplish this: 我可以想到3种方法来实现此目的:
Create a new instance of your AnotherClass
and call the isRaak
method: 创建您的
AnotherClass
的新实例,然后调用isRaak
方法:
public boolean isRaak(int rij, int kolom) { return new Anotherclass().isRaak(rij, kolom); }
Reuse a current instance of your AnotherClass
and call the isRaak
method: 重用
AnotherClass
的当前实例,并调用isRaak
方法:
//field in the class initialized somewhere along your current class... AnotherClass anotherClass = ... public boolean isRaak(int rij, int kolom) { return anotherClass.isRaak(rij, kolom); }
If the method is declared as static
in AnotherClass
, call the method directly from the class without using an instance. 如果方法在
AnotherClass
被声明为static
方法,则不使用实例直接从类中调用该方法。
public boolean isRaak(int rij, int kolom) { return AnotherClass.isRaak(rij, kolom); }
Note that the first two methods assume the constructor of AnotherClass
doesn't need to receive parameters, but this should be adaptable to your specific case. 请注意,前两个方法假定
AnotherClass
的构造函数不需要接收参数,但这应适合您的特定情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.