简体   繁体   中英

Java can't find public static method

I'm working on a programm where a two lists are created and they have to be compared to find if there are two RECURS that are the same. I'm testing if it works (and have to use these methods) but I keep having the same problem; cannot find symbol.

public class Duplicate {

public Duplicate(){}; 

static ArrayList<Recurs> findDuplicate(ArrayList<Recurs> l1, ArrayList<Recurs> l2){
    ArrayList<Recurs> l3 = new ArrayList<>();       
    for(int i=0; i<l1.size(); i++){
        for(int j=0; j<l2.size();j++){
        if(l2.get(i).equals(l1.get(j))){
            l3.add(l1.get(i));
            }  
        }
    }
   return l3; 
  } 
}

This code is supposed to work. By the way, I've programmed a class called Recurs, which supposedly also works (I made another test and that worked ok where I created an equals method).

The problem comes now.


public class Test {

    public static void main (String[] args){
        Recurs o = new Recurs(3, "a"); 
        Recurs e = new Recurs(2, "b");
        Recurs m = new Recurs(4, "a"); 
        Recurs n = new Recurs(2, "b");


        ArrayList<Recurs> l1= new ArrayList<>(); 
            l1.add(o);
            l1.add(e);

        ArrayList<Recurs> l2= new ArrayList<>(); 
            l2.add(m);
            l2.add(n);


        ArrayList<Recurs> l3 = new ArrayList<>(findDuplicate(l1, l2))

    }
}

I create a test where it is supposed to show me that this part is working, but i've got a problem at the last line of code, because it tells me it cannot find findDuplicate.

I'm new at using Java, if someone finds the problem, could they also point out the reason why it is happening?

Static methods "belong" to the class they are written in.

Here:

 findDuplicate(l1, l2)

tries to call a (static) method in the Test class! Because it is an unqualified call, so the compiler will look inside the class that call takes place (which is Test). But there is no findDuplicate() method in the Test class!

Instead, it comes from the Duplicate class, so you need:

Duplicate.findDuplicate(l1, l2)

( alternatively, you could do a static import of that method, but for newbie learning things, I recommend to avoid doing that )

And of course, the method must also be visible to your Test class. So either the two classes should be in the same package, or as mentioned in various comments, that method needs the public modifier.

Without any modifier, your method will be only accessible to its own class, subclasses and the package. To make it accessible to your test, you could make it public but it is customary for test classes to share the same package as the classes they test by adding

package reverse.domain.name.app;

above both your class and your test class.

Secondly, your method is static, which means it is associated to the class, not its instance. this means that you should refer to it by its classname:

Duplicate.findDuplicate()

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