Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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_Methods - Fatal编程技术网

在java中的另一个方法中检索局部变量值

在java中的另一个方法中检索局部变量值,java,methods,Java,Methods,在上面的程序中,我有两个方法setValue()和getValue()。SetValue方法有两个变量x和y,分别为10和20赋值(来自主方法) 现在我想在getValue()方法中显示x和y变量的值。但这是不可能的,因为它们是局部变量。有什么可行的方法吗 有什么可行的方法吗 通常的做法是使它们成为类的字段,特别是实例字段: 使用一个setValue方法设置两个单独的字段相对较少,但也有一些用例。通常你会有setX和setY(和getX和getY)。你可以将它们设置为公共的,或者如果你想使用ge

在上面的程序中,我有两个方法setValue()和getValue()。SetValue方法有两个变量x和y,分别为10和20赋值(来自主方法)

现在我想在getValue()方法中显示x和y变量的值。但这是不可能的,因为它们是局部变量。有什么可行的方法吗

有什么可行的方法吗

通常的做法是使它们成为类的字段,特别是实例字段:


使用一个
setValue
方法设置两个单独的字段相对较少,但也有一些用例。通常你会有
setX
setY
(和
getX
getY
)。

你可以将它们设置为公共的,或者如果你想使用getter和setter,你可以这样做

public class DemoClass {

    private int x;           // These are
    private int y;           // instance fields

    public void setValue(int a, int b)
    {
        this.x = a;
        this.y = b;
    }

    public void getValue()
    {
        // Use `this.x` and `this.y` here
    }

    public static void main(String[] args)
    {
        DemoClass dc=new DemoClass();
        dc.setValue(10, 20);
        dc.getValue();
    }
}
您还可以将信息放入构造函数中,如下所示:

public class DemoClass {
    private int x;
    private int y;
    public void setValue(int a, int b)
    {
        this.x=a;
        this.y=b;
    }
    public void getValue()
    {
        System.out.println(this.x);
        System.out.println(this.y);
    }
    public static void main(String[] args)
    {
        DemoClass dc=new DemoClass();
        tc.setValue(10, 20);
        tc.getValue();
    }
}
这样,当您创建对象时,如:

public class DemoClass {
    private int x;
    private int y;
    public DemoClass() {
        this.x = 0; // default value
        this.y = 0; // default value
    }
    public void setValue(int a, int b)
    {
        this.x=a;
        this.y=b;
    }
    public void getValue()
    {
        System.out.println(this.x);
        System.out.println(this.y);
    }
    public static void main(String[] args)
    {
        DemoClass dc=new DemoClass();
        tc.setValue(10, 20);
        tc.getValue();
    }
}

它已经设置了这些对象。如果您不这样做,并且意外地调用getValue(),但没有设置任何内容,您将得到一个
nullPointerException

否,您只能通过使它们成为类成员使它们成为实例变量来实现
public class DemoClass {
    private int x;
    private int y;
    public DemoClass() {
        this.x = 0; // default value
        this.y = 0; // default value
    }
    public void setValue(int a, int b)
    {
        this.x=a;
        this.y=b;
    }
    public void getValue()
    {
        System.out.println(this.x);
        System.out.println(this.y);
    }
    public static void main(String[] args)
    {
        DemoClass dc=new DemoClass();
        tc.setValue(10, 20);
        tc.getValue();
    }
}
DemoClass demoClass = new DemoClass();