Getter and Setters not working?

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

    here:

    public void setReminderID(long id) {
        id = reminderID;  //<-- wrong, should be the other way around
        System.out.println("Reminder ID Value in Setter: " + reminderID);
    }
    

    You should use reminderID = id instead

    Also:

    public void onCreate(Bundle SavedInstance) {
    
        reminderID = 16;           //one of these two lines..
        setReminderID(reminderID); //..should do it      
    
    }
    

    No need to set the reminderId twice

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

    Thanks for all your help you guys. I wasn't able to figure out why my getters and setters weren't working, even though I followed all of your guys' suggestions. I'm guessing it has something to do with the fact that reminderID is taken in as an argument of a constructor. Either way, I solved my problem by using Android's Bundle and transferring my variables from one activity to another through that.

    Again, thanks for all your help! :)

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

    Your setter should look like this:

     public void setReminderID(long id) {
        reminderID = id;
        System.out.println("Reminder ID Value in Setter: " + reminderID);
    }
    

    Your remainderID variable should be on the left. The way you had it written, id was being set to reminderID.

    Also, in your getter:

    public long getReminderID() {
        return reminderID;
        System.out.println("Reminder ID Value in Getter: " + reminderID);
    }
    

    Since the System.out.println is after the return, then you shouldn't ever see the output. Return will end the function right there.

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