Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts about Getter and Setter syntax  RSS feed

 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, here i'm again, obviously with other noob questions :P This time about Getter and Setter methods.

1- I've a class and an instance variable:



When i use the setBrand method, why i've to use another variable too? Like this:



There is no way to use only the 'brand' variable without the 'aBrand' one?

2- I can use 'aBrand' without previously declaring it in the class, (at least in the example on the book i'm reading). In the example on the book there is no 'aBrand' instance variable declared in the 'Guitar' class.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write

you are declaring aBrand to be a variable of type String, and is is visible only inside the code of setBrand().
(note the addition of the "public" modifier)

You could also do this:

Here, you are declaring a variable "brand" visible in the setBrand() method, that hides the instance variable of the same name.
So you need to refer to the instance variable as "this.brand" to distinguish it from the local variable declared in the calling sequence.

 
Alex Lieb
Ranch Hand
Posts: 61
3
Java Netbeans IDE Notepad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I'll take a shot at this...

So you've got a class that looks like this



Things to know:

Where you declare a variable and how you declare it can limit whether it can be seen from different parts of the program. Specifically, if you have a set of curly brackets, the stuff that's declared inside them can't be seen from outside. So if you have code that does this:


It'll throw an exception on line 4 because once you exit the curly brackets inside which "thing" was created, "thing" is out of reach. Nothing can touch it anymore.

Also, if you make a method, you can give it parameters, which are then only visible inside that method; so if you have two methods that are like this:



You won't be able to compile that. Even if you call doThing first, it'll barf if you try to compile and run it because when you're in the "doAnotherThing" method, parameter1 isn't visible.

But if you have a variable that's created in the class but not in any particular method, all the methods in the class can see that variable.
If you use the "public" modifier, it makes it so stuff *outside* the class can see the variable or method or whatever too.
And for the record, if you declare a variable in a class, but not in a particular method, by default it acts like you used the 'public' modifier
So if you do this like above, but with a "getter" too:



What's actually happening is when you call "setBrand", you're handing "setBrand" a value, and because it's accepting it as a parameter, it stores the value as a "local variable", so "aBrand" is only ever visible inside the method "setBrand". But to store it more permanently, you need to take the value in "aBrand" and store it in "brand", which will still be visible even after "setBrand" is said and done, because after setBrand is done, "aBrand" disappears; nobody can see it anymore. It's just a temporary field you create so you can transfer the information it holds over to the field that's going to store it more permanently.

Then when you call "getBrand", it's able to pull the value you stored earlier because 'brand' is visible to every method in the class.
And since it's public, if you wanted, you could really just do away with the getters and setters entirely and just do this:



And you would set and get it by doing something like this:



If a field or method is public, it means anyone who can see a Guitar can also see and use that method or field.
Because the field would be public, you would be able to just access it directly as long as you could see the guitar object in question.

But for various reasons, it's better to use getters and setters; it gives you a lot more control over how the field is changed, makes it easier to see *when* it's being changed, and generally makes your code easier to read.
It doesn't seem like it would matter much in this example, but if you try to make class scope variables like this without using getters and setters it gets complicated fast.

So your book is probably going to tell you to do something like this eventually:

Make the *variable* private, which means that things in the class can see it, but nothing outside the class can see it.
But make the *getter* and the *setter* public, so in order to look at or change the guitar's brand, you *have* to use the getters and setters.

So you'll probably end up with something like this, but with different field names:

 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!! No doubts anymore! :P

Thanks again Marking post to Resolved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!