Getter返回'0'值;不通过对象?

时间:2017-07-04 11:19:58

标签: java getter-setter

我刚刚开始学习Java,在编写/试验一个简单的程序时遇到了一些问题。

这是从类VDC调用getter,getDateDifference()的方法iM。

 public static void iM() {

    VDC answer = new VDC();
    long theAnswer = answer.getDateDifference();
    System.out.println(theAnswer);

    }

打印出零。

这是VDC课程。

public class VDC {



public static long dateDifference;

    public static void main(String[] args) {
        VDC.dateDifference();
        System.out.println(dateDifference);
    }

    public static void dateDifference() throws ParseException{

        String pattern = "dd/MM/yyyy/HH/mm";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date startDate = format.parse("22/06/2017/15/00");
        System.out.println("Start Date: " + startDate);

        Date dNow = new Date();
        System.out.println("Current date is: " + dNow);
        long diff = dNow.getTime() - startDate.getTime();
        dateDifference = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
    }

    public long getDateDifference() {
        return dateDifference;
    }

    public void setDateDifference(long dateDifference) {
        VDC.dateDifference = dateDifference;
    }
}

我很确定我正确地编写了setter和getter,并且在Eclipse中运行VDC.java为我提供了dateDifference的正确值。但是,如果我尝试运行写入iM方法的类,则dateDifference返回值0.

在google搜索太久之后,我认为我的问题可能是实例dateDifference未设置为源自VDC类的传递dateDifference(对不起,如果我让任何人感到困惑)。

我试图将我在其他SOF问题中找到的内容应用到我的程序中,但我发现自己无法这样做。如何确保getDateDifference()在另一个类中返回正确的值(而不是0)?

(如果这是一个糟糕的问题,我很抱歉;我根本无法应用我发现的解决问题的方法)

2 个答案:

答案 0 :(得分:2)

dateDifference是由静态方法dateDifference()初始化的静态变量。

您永远不会在iM()方法中调用该方法,因此它未初始化。

另一方面,当您运行main类的VDC时,请调用VDC.dateDifference()来初始化该变量。

那就是说,使用返回/修改静态变量的实例getter / setter方法是个坏主意。可以使getDateDifference()setDateDifference()使用静态方法,也可以使dateDifference成为非静态变量。

这是一个可能有意义的实现:

public static void iM() {
    VDC answer = new VDC();
    answer.computeDateDifference(); // initialize the instance variable
    long theAnswer = answer.setDateDifference(); // get its value
    System.out.println(theAnswer);
}

public class VDC {

    private long dateDifference;

    public void computeDateDifference() throws ParseException {

        String pattern = "dd/MM/yyyy/HH/mm";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date startDate = format.parse("22/06/2017/15/00");
        System.out.println("Start Date: " + startDate);

        Date dNow = new Date();
        System.out.println("Current date is: " + dNow);
        long diff = dNow.getTime() - startDate.getTime();
        this.dateDifference = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
    }

    public long getDateDifference() {
        return dateDifference;
    }

    public void setDateDifference(long dateDifference) {
        this.dateDifference = dateDifference;
    }
}

答案 1 :(得分:0)

您永远不会同时调用setDateDifferencedateDifference。 因此,您需要将iM更改为

public static void iM() {
    try {
        VDC.dateDifference();
        long theAnswer = answer.getDateDifference();
        System.out.println(theAnswer);
    catch (ParseException e) {
        ...
    }
}

由于您的方法是静态的,因此您无需创建VDC的实例。

相关问题