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

问题描述 投票:-1回答:2
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)'

您可以使用此作为练习来调用方法。

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

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

java
2个回答
0
投票

这条线

Methods.getvar(varpass);

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

这条线

call.getvar(varpass);

工作良好。它实际上是第一行的解决方案,您可以从静态上下文中裁判非静态方法


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),因为没有具有该名称的局部变量。

© www.soinside.com 2019 - 2024. All rights reserved.