Results 1 to 9 of 9
Thread: Boolean setter Method
- 01-18-2011, 11:15 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
Boolean setter Method
Hi first timer here
I want to create a setter method for a boolean instance I have declared.
I want it to change the value to false if it is already true and vice versa
The compiler is giving me some syntax errors when I write it though
Here is my method
/**
* Setter method for lastGiven
*/
public boolean setLastGiven()
if (getLastGiven == false);
{
return lastGiven = true;
}
else
{
return lastGiven = false
lastGiven is the instance variable I have declared as follows
private boolean lastGiven;
I have also initialised it as follows
this.lastGiven = false;
Hope you can help
- 01-18-2011, 11:17 PM #2
Why are you returning a value from a setter method?
Java Code:bool = ! bool;
-
setters shouldn't return anything, making your code and your goals confusing. If you want to create a setter for a boolean, have it return void and pass a boolean parameter into it that is used to set the variable. It sounds like you don't want to create a setter method but rather want to toggle the boolean field, and if so, I'd perhaps call the method toggleLastGiven(), but would also have it return void since the goal of the method is not to return a result but to change an object's state. To toggle a boolean, simply do this:
Java Code:myBoolean = !myBoolean;
Also, please read my link on use of code tags here. Also, if your code causes errors or exceptions, you're going to want to post the actual error message.
Luck.
- 01-18-2011, 11:51 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
Ok thanks thats beginning to make sense now, sort of.
I've changed to this
/**
* Setter method for lastGiven
*/
public void setLastGiven()
{
lastGiven = !lastGiven;
}
(I'm just going look at your Links for posting sorry if it's not the best yet)
Think I was getting confused.
Thanks for your help
Jon
-
Again, that's not a setter method and should not be called setLastGiven. A setter method takes the parameter and sets the field based on the parameter. This method toggles a boolean and its name should reflect that it does this. Again, please read my link on use of code tags here. Your posted code is very difficult to read without tags.
- 01-19-2011, 04:50 AM #6
Hey JonoHart,
Go through this link first which explains about accessors and mutators methods of java. How to use them and why to use them. Here is the link : Java Accessors and Mutators.
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-19-2011, 08:02 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
Ok got it, changed the code again and tested as working.
Thanks for all your helpJava Code:public void setLastGiven(boolean lastGiven) { this.lastGiven = lastGiven; }
Jon
- 01-19-2011, 08:27 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
-
Similar Threads
-
getter and setter method help please!
By merdzins in forum New To JavaReplies: 2Last Post: 12-06-2010, 05:06 AM -
Boolean and Method help on Homework
By gto400no1 in forum New To JavaReplies: 3Last Post: 02-22-2010, 12:12 AM -
private static setter method problem
By kyussy in forum New To JavaReplies: 7Last Post: 01-27-2010, 04:02 PM -
Null Pointer Exception via setter method?
By zerkz in forum New To JavaReplies: 9Last Post: 10-29-2009, 09:00 PM -
Boolean method help
By syferite in forum New To JavaReplies: 6Last Post: 10-28-2009, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks