简体   繁体   English

尝试调用从另一个类调用另一个方法的方法时出现StackOverflowError

[英]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种方法来实现此目的:

  1. 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); } 
  2. 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); } 
  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM