Java 8 reference to a static method vs. instance method

后端 未结 2 1008
无人共我
无人共我 2021-02-19 19:08

say I have the following code

public class A {
    int x;
    public boolean is() {return x%2==0;}
    public static boolean is (A a) {return !a.is();}
}
         


        
2条回答
  •  旧时难觅i
    2021-02-19 19:39

    We can not use not static methods or non-global methods by using className::methodName notation. If you want to use methods of a particular class you have to have an instance of the class.

    So if you want to access is() method then you can use : 
    A a = new A();
    a.is();
    OR 
    (new A()).is();
    

    Thanks.

提交回复
热议问题