无法解决方法练习的方法错误

时间:2018-03-04 07:09:40

标签: java

public class MainForm {
    String varpass = "This is a string that has to be passed.";
    public String t1(){
        String text = "This is a non-static method being called.";
        return text;
    }
    public static String t2(){
        String text = "This is a static method being called.";
        return text;
    }
    public void t3(){
        System.out.println("This is a non-static void method and cannot return.");
    }
    public static void t4(){
        System.out.println("This is a static void method and cannot return.");
    }
    public void place1 (){
        //=======================================Method calls from another class========================================
        //Calls from another class. It is non-static and thus requires it to be instantiated. EG. class var = new class();
        Methods call = new Methods();
        System.out.println(call.t1());

        //Calls from another class. It is non-static void and thus requires it to be instantiated and be called straight.
        call.t3();

        //Calls from another class. It is static and thus does not require it to be instantiated. EG. class var = new class();
        System.out.println(Methods.t2());

        //Calls from another class. It is static void and thus does not require it to be instantiated.
        Methods.t4();

        //Trying to call a variable that was sent.
        Methods.getvar(varpass);
        call.getvar(varpass);

        //=======================================Method calls from current class========================================
        MainForm mcall = new MainForm();
        //Calls from within the same class. It is static and thus does not require it to be instantiated. EG. class var = new class();
        System.out.println(mcall.t1());
        mcall.t3();
        System.out.println(t2());
        t4();
    }
    public static void main(String[] args) {
        MainForm place = new MainForm();
        place.place1();
    }
}

public class Methods {
    String var1 = "This is a public String variable";
    String getVar = "Initial";
    public String t1(){
        String text = "This is a non-static method being called.";
        return text;
    }
    public static String t2(){
        String text = "This is a static method being called.";
        return text;
    }
    public void t3(){
        System.out.println("This is a non-static void method and cannot return.");
    }
    public static void t4(){
        System.out.println("This is a static void method and cannot return.");
    }
    public void getvar(String varsent){
        String msg = "getver() Variables are varsent("+varsent+"), getVar("+getVar+"), getVar(";
        getVar = varsent;
        msg = msg + getVar+")";
        System.out.println(msg);
    }
}

以下是

中的错误
Methods.getvar(varpass);
call.getvar(varpass);

排名第一的是非静态无法从静态上下文中引用

底部是说不能解决方法' println(void)'

你可以告诉im使用它作为练习来调用方法。

这里我试图传递一个包含字符串的变量varpass。我希望它将该变量传递给方法中的getvar。在那个getvar中它接受一个变量在方法中显示它然后再改变它然后再改变。

我试图了解这是如何工作的。任何见解都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您不能从静态方法引用非静态变量/字段,因为非静态字段可能因类的实例而异。要解决它,请使用varpass static

static String varpass = "This is a string that has to be passed.";

第二个错误来自getvar的定义:

public void getvar(String varsent);

由于它没有返回任何内容,因此不能在System.out.println()中使用它,因为没有println接受void的定义(它不知道要打印什么)。

此外,Methods.getvar(varpass)应为Methods.getvar(MainForm.varpass),因为没有具有该名称的本地变量。

答案 1 :(得分:0)

这一行

Methods.getvar(varpass);

是静态调用。您尝试从Methods类调用静态方法getvar。然而,这种方法不是静态的,因此需要一个Method的实例。

这一行

call.getvar(varpass);

工作正常。它实际上是第一行的解决方案,您可以从静态上下文中引用非静态方法