Getter and Setters not working?

后端 未结 9 2008
悲&欢浪女
悲&欢浪女 2021-01-27 02:12

I\'ve got two classes right now: RemindersDAO.java and ViewLocalReminders.java.

I\'m trying to get access to a variable that\'s in ViewLocalReminders.java and I\'m tryin

9条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-27 02:23

    I see that you accepted the answer. However, I think I know the problem about your getter-setter bug. It's about the order and order matters...it's like you're giving the instruction step-by-step.

    Let me explain...

    The first one, your setter method, did a mistake. (See marked comment/s)

    public void setReminderID(long id) {
            id = reminderID; // --> your argument is id. reminderId field value should be changed not the other way around.
            System.out.println("Reminder ID Value in Setter: " + reminderID);
        }
    

    Here's the correct setter.

    public void setReminderID(long id) {
        reminderID = id; // --> "id" is your parameter and that's the reminderID's new value.
        System.out.println("Reminder ID Value in Setter: " + reminderID);
    }
    

    Next, about the getter. The reason why there's a bug in getter is because you have a wrong order. (See marked comment/s)

    public long getReminderID() {
        return reminderID;
        System.out.println("Reminder ID Value in Getter: " + reminderID); // --> putting this line code after "return" results an error and therefore you won't be able to obtain a number or value!
    }
    

    Here's the correct getter. Look carefully the difference.

    public long getReminderID() {
        System.out.println("Reminder ID Value in Getter: " + reminderID); // --> Anycode before "return" value will be fine.
        return reminderID; // --> Always put the "return" value at the last line within a getter method.
    }
    

    I hope you can use this correction for your future project in case whenever you need to apply the getter-setter method when you're programming. Remember that it's very important because ordering of the code matters and know where you direct the value changes. Questions?

提交回复
热议问题