2

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?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Smpl Fcts
  • 55
  • 1
  • 5
  • 1
    `Duplicate.findDuplicate(l1, l2)` – Elliott Frisch Apr 16 '19 at 20:05
  • 3
    It's not `public` – Andronicus Apr 16 '19 at 20:05
  • `static ArrayList findDuplicate` should have `public` in front of it. – Nick Vitha Apr 16 '19 at 20:08
  • 1
    Combining the above, you need to mark the findDuplicate() method public and call it as a static method, ie `Duplicate.findDuplicate(...)` – BadZen Apr 16 '19 at 20:08
  • 1
    And a hint: prefer to use `List` instead of ArrayList where possible. You **only** need to give the name of the class implementing the interface for that `new()` statement! See https://stackoverflow.com/questions/1992384/program-to-an-interface-what-does-it-mean for example. – GhostCat Apr 16 '19 at 20:10
  • Right now it has package protection which means any classes in the same package can call it so he may not have to declare it public. – Bakon Jarser Apr 16 '19 at 20:11

2 Answers2

2

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

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()
Mr. Wrong
  • 500
  • 7
  • 21