• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getters and setters  RSS feed

 
Mark Holland
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I'm learning Java using Kathy Sierra's excellent book 'Head First Java'. I'm having difficulty understanding the getters and setters and I hope someone can help me out here.

A setter takes an argument value and uses it to set the value of an instance variable. Yet I don't understand the following example code:



Thanks a lot in advance for explaining!

Regards
Mark


 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"s" just happens to be a poorly chosen variable name for an int. I suppose it's "s" for "size" but I would have just used "size" instead.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am not the most experienced guy with Java but I'll share what I know,

lets start of with a simple class




we already set the number in the constructor because it's good practice,also notice the set method is void because we are not returning anything.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


sorry heres the updated code because a main method would be useful haha
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the getSize method is like asking the question, “How large are you?”
The setSize method is a bit harder to envisage; it is like saying, “You are now ??? large.” Or, maybe better, “How large would you like me to be?”

So, is your problem, “Why s?”
I think s is short for size there. Maybe something like newSize would have been better. But you can't write a method like this:-The problem is in line 4. The compiler will complain that you have to write an identifier not 123. But that isn't how a method parameter works. You say what sort the parameter is (int) and you don't say what value it has; other code will send the value. Only the name you are giving it for the duration of the method. You are saying, “I want a number and during this method I am calling it s.”
You can call the number something different if you want. You can writein which case the number sent to your method is applied to the variable temperature. The compiler and the JVM aren't to know what a daft name that is for a size.
Other code can call your method with myDog.setSize(123); which makes s equal to 123, or you can call it with myDog.setSize(-1234567890); which the compiler and JVM will happily run. You can take precautions against such ridiculous values, but that is for another thr‍ead when you have a little more experience.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark, just a note on your post:  always UseCodeTags (that's a link) when you're posting code.  I've done it for you this time.  Also, make sure your code is formatted properly (indented and such).  Thanks.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The "s" in this code is a parameter.  That means it's a variable that "stands in" for the number you pass to setSize().  The "s" in public void setSize (int s) { is the same as the "s" in size = s;.  When you pass in a number like 70, like this: setSize(70); the "s" gets the value 70.  But if you pass in 123, "s" is set to that number.  You can think of "s" as a placeholder for whatever is passed to setSize().
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that to use a setter method, you always need to write a getter method first?

No.  A field with a setter and no getter would be a write-only field.  More commonly, you will see a getter without a setter.  This would be a read-only field.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!