Results 1 to 9 of 9
  1. #1
    JonoHart is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Why are you returning a value from a setter method?
    Java Code:
    bool = ! bool;

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    JonoHart is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    JonoHart is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Ok got it, changed the code again and tested as working.

    Java Code:
       public void setLastGiven(boolean lastGiven)
       {
          this.lastGiven = lastGiven;
       }
    Thanks for all your help

    Jon

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JonoHart View Post
    Ok got it, changed the code again and tested as working.

    Java Code:
       public void setLastGiven(boolean lastGiven)
       {
          this.lastGiven = lastGiven;
       }
    If you really want to flip the value of that boolean member, why not just do this?

    Java Code:
    public void toggleLastGiven() {
       lastGiven= !lastGiven;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    If you really want to flip the value of that boolean member, why not just do this?

    Java Code:
    public void toggleLastGiven() {
       lastGiven= !lastGiven;
    }
    Yes, Jos is stating what I am clumsily trying to tell you, but in a much clearer way. Please heed his advice.

Similar Threads

  1. getter and setter method help please!
    By merdzins in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 05:06 AM
  2. Boolean and Method help on Homework
    By gto400no1 in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 12:12 AM
  3. private static setter method problem
    By kyussy in forum New To Java
    Replies: 7
    Last Post: 01-27-2010, 04:02 PM
  4. Null Pointer Exception via setter method?
    By zerkz in forum New To Java
    Replies: 9
    Last Post: 10-29-2009, 09:00 PM
  5. Boolean method help
    By syferite in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 01:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •