简体   繁体   中英

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

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.

Another suggestion, java naming convention, method name starts with small case letters.

Do not use static unless you are sure it is appropriate.

This is a popular programming error, partially because eclipse keeps on suggesting to make variables and methods static when they cannot be accessed. 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. So it cannot be compiled, because there is no method clustering(String[] args) .

Seriously, you need to learn more Java basics. Maybe from a book .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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