简体   繁体   English

Java找不到公共静态方法

[英]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. 我正在一个程序中创建两个列表,并且必须将它们进行比较以找到是否有两个相同的RECURS。 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). 顺便说一句,我已经编写了一个称为Recurs的类,该类也应该可以工作(我做了另一个测试,在创建equals方法的地方工作得很好)。

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. 我创建了一个测试,该测试应该告诉我该部分正在工作,但是我在最后一行代码中遇到了问题,因为它告诉我找不到findDuplicate。

I'm new at using Java, if someone finds the problem, could they also point out the reason why it is happening? 我是使用Java的新手,如果有人发现问题,他们还能指出发生问题的原因吗?

Static methods "belong" to the class they are written in. 静态方法“属于”它们所写入的类。

Here: 这里:

 findDuplicate(l1, l2)

tries to call a (static) method in the Test class! 尝试在Test类中调用一个(静态)方法! Because it is an unqualified call, so the compiler will look inside the class that call takes place (which is Test). 因为这是一个不合格的调用,所以编译器将在调用发生的类(即Test)的内部进行查看。 But there is no findDuplicate() method in the Test class! 但是Test类中没有findDuplicate()方法!

Instead, it comes from the Duplicate class, so you need: 相反,它来自Duplicate类,因此您需要:

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. 当然,该方法也必须对您的Test类可见。 So either the two classes should be in the same package, or as mentioned in various comments, that method needs the public modifier. 因此,这两个类应该位于同一包中,或者如各种注释中所述,该方法需要public修饰符。

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()

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

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