简体   繁体   English

从void Java调用静态方法时出错

[英]error on calling static method from void java

Need help on calling a method from main class. 在从主类调用方法时需要帮助。

I need to call a method, thus I made an object to handle it. 我需要调用一个方法,因此我创建了一个对象来处理它。
below I quote my main method 下面我引用我的主要方法

 public static void main(String[] args) {
        // TODO code application logic here
        SLRatio sl= new SLRatio();
        sl.clustering(apa);
}  

and here's the method I need to call 这是我需要调用的方法

public class SLRatio {

public static String [][]clustering(String[][]apa) {

   System.out.println("Cluster 1");

   int a = apa.length/3;
   String [][] cluster1=new String [a][apa[0].length];

   for (int i =0; i<a; i++) {
      for (int j=0;j<apa[0].length;j++) {
         cluster1 [i][j] = apa[i][j];
      }
   }

   for (int b = 0; b < cluster1.length; b++) {
      for (int c = 0; c < cluster1[0].length; c++) {
         System.out.print(cluster1[b][c] + "\t");
       }

       System.out.println("");        
   }

   System.out.println("\n");

   return cluster1;

}
}

and I got error message: "Cannot find symbol,Accessing static method clustering" 我收到错误消息:“找不到符号,正在访问静态方法集群”

What can I do to solve it? 我该怎么解决? I have tried to change the syntax but it didn't work. 我试图更改语法,但是没有用。
Thank you so much. 非常感谢。

you didn't define method Allocation() in SLRatio 您没有在SLRatio定义方法Allocation()

Note: static method should be called with classname (to avoid confision between instance method and static) 注意:应使用类名调用静态方法(以避免实例方法与静态方法之间的冲突)

If it is static method, you don't need to call it through instance. 如果它是静态方法,则无需通过实例调用它。

SLRatio .clustering(...);

should be enough. 应该足够了。

And it seems you forgot to implement Allocation method. 看来您忘记了实施Allocation方法。

Another suggestion, java naming convention, method name starts with small case letters. 另一个建议是Java命名约定,方法名称以小写字母开头。

Do not use static unless you are sure it is appropriate. 除非确定合适,否则不要使用static

This is a popular programming error, partially because eclipse keeps on suggesting to make variables and methods static when they cannot be accessed. 这是一个流行的编程错误,部分原因是eclipse一直建议在无法访问变量和方法时使它们变为static But usually, this is not the correct solution. 但是通常,这不是正确的解决方案。 While it fixes the compilation problem, it often breaks the application logic. 它解决了编译问题,但通常会破坏应用程序逻辑。

Right now, your problem probably is that apa has type String[][] , but you are passing a String[] parameter to it. 现在,您的问题可能是apa具有String[][]类型,但是您正在向其传递String[]参数。 So it cannot be compiled, because there is no method clustering(String[] args) . 由于没有方法clustering(String[] args) ,因此无法编译。

Seriously, you need to learn more Java basics. 认真地说,您需要学习更多Java基础知识。 Maybe from a book . 也许是一本书

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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