Getter and Setters not working?

后端 未结 9 2005
悲&欢浪女
悲&欢浪女 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:19

    You're assignment of the reminder id is backwards

    id = reminderID;
    

    should read

    reminderID = id; 
    

    in you setReminderID method

    0 讨论(0)
  • 2021-01-27 02:22

    you have

    id = remainderID;
    

    it should be

     remainderID = id;
    
    0 讨论(0)
  • 2021-01-27 02:22
    public void setReminderID(long id) {
        this.reminderID = id;
        System.out.println("Reminder ID Value in Setter: " + reminderID);
    }
    
    0 讨论(0)
  • 2021-01-27 02:22

    Your setter method seems wrong. You need to use your IDE's auto generation to create these methods.

    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2021-01-27 02:25

    try using this

    this.remainderId = id;
    

    instead of

    remainderId = id;
    

    in your model or domain.

    0 讨论(0)
提交回复
热议问题