Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给定的Java代码有什么问题_Java - Fatal编程技术网

给定的Java代码有什么问题

给定的Java代码有什么问题,java,Java,代码是 我正在使用EclipseLunaIDE和JDK8。。。 您能告诉我为什么编译器在这里显示错误吗。。。。。 无法从类型ctorsandobjs对非静态方法addint int进行静态引用 我是JAVA新手 如果可能的话,建议一个解决方案您不能引用非静态成员私有inta;静态函数中的公共int b add方法不是静态方法,因此需要在类ctorsandobjs的实例上调用它,例如: public class ctorsandobjs { private int a; publi

代码是

我正在使用EclipseLunaIDE和JDK8。。。 您能告诉我为什么编译器在这里显示错误吗。。。。。 无法从类型ctorsandobjs对非静态方法addint int进行静态引用

我是JAVA新手


如果可能的话,建议一个解决方案

您不能引用非静态成员私有inta;静态函数中的公共int b

add方法不是静态方法,因此需要在类ctorsandobjs的实例上调用它,例如:

public class ctorsandobjs {
    private int a;
    public int b;

    public ctorsandobjs(String arg)
    {
        System.out.println("I got " + arg);
    }

    public void add(int a,int b)
    {
        System.out.println("Addition is " + String.valueOf(a+b)); 
    }
    public static void main(String args[])
    {
        ctorsandobjs c = new ctorsandobjs("You");
        c.a = 12;
        c.b = 15;
        add(c.a,c.b);                      //compiler shows error here
    }
}
add是一个非静态方法,因此必须从类的对象调用它 你必须做到:

c.add(c.a,c.b);

请原谅我的无知,谢谢你
c.add(c.a, c.b);