Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Get and Set method  RSS feed

 
Justin Tan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to understand the get and set methods. When a value for a variable for has already been given, and a getName() is invoked on the object, the name on the object is returned? For instance if the current value for the variable for name is "Apple", getName() will return Apple when invoked on that object? And we use setName() on the same object to replace the value of name to another value such as "Orange". Why do we create another variable to hold the previous value after we use the setName() method to replace the existing value of that variable? Thank you.

 
Junilu Lacar
Sheriff
Posts: 11152
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 66 to 68 only make sense if they are in the Student class.  Lines 70 and 72 would make more sense if they were inside the main() method of the StudentTest class.  As you have shown them, however, they would not even compile.

Now, assuming the above were so, and assuming that the Student class actually had a setName() method, because the code that you posted doesn't, then the answer to your question "Why do we create another variable to hold the previous value after we use the setName method to replace the existing value of that variable?" would be:

1. In the first place, if line 70 were executed before line 71, then you're actually creating another variable to hold the previous name before you replace it with another value.
2. As to the question why you would do that, in this case it's just an example, presumably so that you can print it out and see that indeed, the old name was returned by the getName() method.

A more complete listing would be something like:

The output from the above code would be:

S2 used to be known as Sai
Now S2 goes by the name Peter

The reason you need to keep the old name in another variable is because a variable like name can only have one value at a time. Once you set it to a value, its old value is lost, unless you save it in another variable.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.  Welcome to the ranch.


>When a value for a variable for has already been given, and a getName() is invoked on the object, the name on the object is returned?
>For instance if the current value for the variable for name is "Apple", getName() will return Apple when invoked on that object?

By convention and in this case, yes. That is what the method does.


>And we use setName() on the same object to replace the value of name to another value such as "Orange".
Yes. 
I don't see a setName() method anywhere in your code, but the standard one would look something like:



>Why do we create another variable to hold the previous value after we use the setName() method to replace the existing value of that variable?
There is no necessity to do so.
I guess they are just showing examples of what you can do with java bean classes.

Sometimes there is a reason to keep a hold of that value - if you want to know the before and after.
More often than not, you won't need to.  It all depends on the program you are writing.



 
Justin Tan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu Lacar and Stefan Evans. It helped me to understand the concept better. Another thing is that as you can see that there are 3 constructors in my class. If the class has 10 variables, a total of 11 constructors can be created for the class. Do we need to include all the possible type of constructors in the class first before we create an object by calling the constructor in the class (i.e. even the constructors which we will not use) or do we only create the constructors that we need in the class after we create the object by calling that particular constructor .



Based on the code above, if i want to change the name for object s2,
1) s2.getName()              //To get the current name stored in s2.
2) s2.setName(String Justin)                   // To change the value in name.

Is that correct?

Lastly, if i want to change the name and roll number  instead of just name, will the following code below will be correct? Thank you.


 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
11 constructors for a class seems a little excessive.
I would only include the ones that "make sense"
And I would probably limit that to 2-3 constructors in most cases.

Also, when there are multiple constructors, I would prefer calling one constructor from another rather than repeating the "work" that each one does.
For example rather than the contructor with no parameters setting variables, I would invoke another constructor with the this keyword.

i.e.

The result is that only one of your constructors actually ends up doing the work - the rest of them just provide default values.
 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Tan wrote:If the class has 10 variables, a total of 11 constructors can be created for the class.


Well, no, you can create up to 1024 constructors if your class has 10 attributes. You could create one which sets attributes number 3, 6, and 7. You could create one which sets attributes number 1, 2, 4, 8, and 9. And so on... there are 1024 possibilities here.

Now obviously that many constructors is ridiculous. So have a look at this article by Joshua Bloch (remember that name, it's somebody who knows what they are talking about in the world of Java): Consider a builder when faced with many constructor parameters.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!