-2

I was given a problem to solve:

A Book, which as a title, author, and year of publication. Include methods to get and set the private instance variables and a toString method to display the object. Also create a method moreRecent which takes two books as input parameters and returns the one that was published more recently. Create 3 JUnit tests for moreRecent.

I think the part about creating a method "moreRecent" is easy enough, but I don't get what the problem means by getting and setting the private instance variables and a tostring method to display the object. Does anybody know what it means and show me, because I am really confused. Thanks a lot!

Thanks, I believe I solved it.

Freedom
  • 347
  • 2
  • 7
  • 24
  • You could easily google what an instance variable is. – Sotirios Delimanolis Dec 17 '13 at 15:39
  • I tried googling it and I'm still confused what it means. I don't want/need you to solve it, just to explain what it means to set a private instance variable – Freedom Dec 17 '13 at 15:40
  • Setting a private instance variable means setting a private instance variable. That's a primitive concept of Java. Go through the Oracle Trail tutorials. – Sotirios Delimanolis Dec 17 '13 at 15:42
  • 1
    Can you reword your question to boil it down to what you don't understand and what you need help with? People tend to jump to dismiss questions that look like homework help. Your second paragraph seems to be the crux of the matter. – yonitdm Dec 17 '13 at 15:42
  • This question should be in stackexchange, stackoverflow is for help with code. – RealityDysfunction Dec 17 '13 at 15:44
  • So is an private instance variable like a variable that is in a class and can only be used in that class? Thanks – Freedom Dec 17 '13 at 15:44
  • Instance variable == non-static member variable; member variable == a variable that's in the class (not a local variable). See [Declaring Member Variables](http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html). – Jesper Dec 17 '13 at 15:47
  • Ok thank you, sorry I'm sorta new to defining new classes and such. Thanks. – Freedom Dec 17 '13 at 15:49

1 Answers1

1

An instance variable is a variable that can be unique for each instance of the class.

A variable that is not an instance variable will be shared by all instances of the class.

If a variable is declared static it will be shared by all instances of the class, if the variable is not declared as static it will be unique for each instance of the class, thus an instance variable.

Declaring a non-static variable as private will not allow other classes to access it directly, thus a private instance variable.

Thus in order to access it you must create (usually public) getter and setter methods within that class to allow other classes to access those private instance variables in a way you define in the getter/setter methods instead of allowing full control over the variable you can choose to put up limits etc within the methods.

As for the toString method, it's a method of the Object class (which is the base class of all other classes) and you can override it to suit your needs. (You can look that up in google)

Ceiling Gecko
  • 3,104
  • 2
  • 24
  • 33