• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java - Final Version of DotCom class pg 150  RSS feed

 
Joe Sarge
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So I am a little confused. This may be a very general questiion but I don't understand why there is a setter method here for name:

public void setName (String n) {
name = n;
}

The reason why I am confused is because "n" is not referenced anywhere else in any of the 3 classes that go to this code. Maybe I need a further explanation on the uses of a setter method?
There are, however, references to "setName" in another class but it just seems like this setter was just thrown in with no purpose. I mean why use this setter if the names are already set in a previous class?
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Sarge wrote:The reason why I am confused is because "n" is not referenced anywhere else in any of the 3 classes that go to this code. ...Maybe I need a further explanation on the uses of a setter method?

Joe, it looks like you have got it wrong ..; Explain us your current understanding on the use of setter methods... that would help us to point out the issues in your understanding.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A setter method is nothing more than a normal method. It is usually used for setting a value to an instance variable. In this case the value of n will be stored in name. Furthermore the scope of n is limited to the method itself. So no matter what you call it, you'll only need to look to this method.
 
Joe Sarge
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's just it. I always thought a setter was used to set a value. Why would I be setting the value of n to the same value of name if n is never used in any of the code? I just finished with all 3 classes and with the exception of this one line, I don't see n anywhere else. So my question is, why is it there? Now I was thinking, in the way it is being used, is this a way to make the name of type String? Basically using a setter method to do it and due to java syntax rules, a letter was used to more or less fill in the blank space that would have been there? I don't know. maybe I am overthinking it. lol
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable n is there to tell the compiler that the caller will pass in a string, and to give a name to the string that is passed in. That gives the method a way to refer to the passed-in variable. The caller doesn't have to name his variable "n"; he can use any legal name he wants, or pass in a literal ("Bob") or the result of some other method call ("databaseValue.getFirstName()").

The purpose of the setter method is to take whatever is passed in and assign it to the instance variable "name".

After it has done that, "n" has nothing left to do. It is not stored with the object, since it is not an instance variable; it only has existence while the method is running.

In more technical terms, the storage for "n" is on the "stack"; this storage is created when the method is called, and after the method returns, is used no longer by anyone. I can go into more detail on that if you're really interested.

Now, this is not to say that "n" could not have another use if the program were changed. Perhaps you will someday want to ensure that the name stored will pass a test using "==" with literals or with another "intern" variable, and will change it to this:

public void setName(String n)
{
name = n.intern();
}

or do something else with "n" before the assignment is made.

Does that help?
rc
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Sarge wrote:Well that's just it. I always thought a setter was used to set a value. Why would I be setting the value of n to the same value of name if n is never used in any of the code? I just finished with all 3 classes and with the exception of this one line, I don't see n anywhere else. So my question is, why is it there? Now I was thinking, in the way it is being used, is this a way to make the name of type String? Basically using a setter method to do it and due to java syntax rules, a letter was used to more or less fill in the blank space that would have been there? I don't know. maybe I am overthinking it. lol


I think you are thinking in reverse. That setter is setting the variable "name" to whatever value is being passed into the method, represented as n. Once name has taken the value of n, n is no longer needed. Are you using "name" anywhere else in the code?
 
Death Arte
Greenhorn
Posts: 21
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Good explanation everyone...
 
Joe Sarge
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AHHHHHHHHHHHHHHHHHHHHHHHHHH.............. Got it! I was sort of on the right track. This forum is a such a good tool. Thanks Guys! I'm sure I'll be posting again....lol
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!