Search code examples
javaandroidgetter-setter

Getter and Setters not working?


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 trying to call it from RemindersDAO.java. I'm doing this by using the getter/setter method combo. However, for some reason, my variable value keeps getting set to 0 in the getter method. Here's the code:

ViewLocalReminders.java

public class ViewLocalReminders extends SherlockListActivity {

private long reminderID;

    public void onCreate(Bundle SavedInstance) {

    reminderID = 16;
    setReminderID(reminderID);      

    }

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

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

}

RemindersDAO.java

public class RemindersDAO extends SQLiteOpenHelper {

    @SuppressWarnings("deprecation")
    public Cursor getRowByID(Activity activity) {
    String[] from = { _ID, NAME };

    ViewLocalReminders viewLocalReminders = new ViewLocalReminders();
    long reminderID = viewLocalReminders.getReminderID();
    System.out.println("Value of Reminder ID in RemindersDAO: " + reminderID);

    }

}

This is the output that I'm getting for all my System.out.printlns:

  1. Reminder ID Value in Setter: 16
  2. Reminder ID Value in Getter: 0
  3. Reminder ID Value in RemindersDAO: 0

So obviously, something's happening between the setter and getter methods. The value of reminderID is 16 in the setter method, the way it should be. However, the value goes to 0 in the getter method. Consequently, the value is still 0 when I call the getter method in RemindersDAO.java. What am I doing wrong here? Thanks for your help! :)


Solution

  • 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! :)