This discussion is archived
2 Replies Latest reply: Feb 9, 2013 2:24 PM by rp0428 RSS

Can't get my class's getter method to work properly.

990161 Newbie
Currently Being Moderated
Hello, I am new to Java programming, although I come from C and C++. This is my first post. I am working on a basic Library application that can Checkout and Return books. Eventually I would like to add the ability to read from and write to an XML file containing data for every book.

My problem right now is that I cannot get my getter method within my Book class to work properly. When I call getData() from within main, and pass the appropriate arguments, instead of filling the variables with that object's data, it leaves them blank! Please help me.


Here is my method within the Book class, the person and id_num are private.
/* Gets general Book Instance Data and fills parameters with it */
     public void getData(String n, int num, String c, String d)
     {
          n = this.person;
          num = this.id_num;
          c = this.checkout_date_string;
          d = this.due_date_string;
     }
Here is how I am attempting to use it in main:
String name = "";
int number = 0;
String checkout = "";
String due = "";

Book book1;

/*Sets person = John Smith and id_num = 10763*/
book1 = new Book("John Smith", 10763);


/*Gets Checkout (todays) date and sets a due date in 2 weeks*/
book1.Checkout();

book1.getData(name, number, checkout, due);

System.out.println("Book1:\n Checkout Date: " + checkout +
                         "\n Due Date: " + due + "\n Is it late: " + book1.isLate() +
                         "\n Person who checked it out: " + name + "\n ID number: " + number +
                         "\n ");
Then, Here is the Output:

Book1:
Checkout Date:
Due Date:
Is it late: false
Person who checked it out:
ID number: 0

Edited by: 987158 on Feb 9, 2013 11:52 AM
  • 1. Re: Can't get my class's getter method to work properly.
    990161 Newbie
    Currently Being Moderated
    Well I went ahead and made separate methods to return each value. I suppose that will work just as well, especially since now I don't have to declare a bunch of strings and int's to hold the data and pass to the method.
  • 2. Re: Can't get my class's getter method to work properly.
    rp0428 Guru
    Currently Being Moderated
    >
    Well I went ahead and made separate methods to return each value. I suppose that will work just as well, especially since now I don't have to declare a bunch of strings and int's to hold the data and pass to the method.
    >
    Well this would never work anyway
    public void getData(String n, int num, String c, String d)
    None of those can be changed within the method. Strings are immutable and 'int' is passed by value. Each of those parameters will have the same value after the method exits as they did before it was called.

    Your book class should be using 'setters' to set the value of its instance variables.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points