Results 1 to 11 of 11
  1. #1
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default Get and Set Methods

    I wonder why if i put the method inside the get method, it will actually do what you expected? However, if i put it inside the set methods, it won't works.

    For Example:

    public class Example
    {
    String number;

    public Example (String t){
    number = t;
    }

    public void setNo(String t){
    number = t;
    }


    public String getNo(){

    if(number.length() == 4){

    number = "2";
    return number;
    }
    return number;
    }

    }

    public class TestExample{

    public static void main(String[] args){

    Example e = new Example("1234");
    System.out.println(e.getNo());
    }
    }

    Result: 2 (Expected Result)


    public class Example
    {
    String number;

    public Example (String t){

    number = t;
    }

    public void setNo(String t){

    if(t.length() == 4){

    t = "2";
    number = t;
    }
    number = t;
    }


    public String getNo(){

    return number;
    }

    }

    Result: 1234 (Not expected)

    Is it normal or my steps are wrong? Why we need to use the get and set methods when we can simply create any method such as "public int display()".....
    Last edited by kian_hong2000; 08-25-2008 at 08:53 AM.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Invoke your setNo() method first before you can get the expected value.......

    Maybe you couldn't appreciate get/set methods for that kind of experiment....

    If you are developing your own API.... that may helps(Convention)
    Last edited by sukatoa; 08-25-2008 at 09:10 AM.
    freedom exists in the world of ideas

  3. #3
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    What do you mean by invoke the method first in this example?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Result: 1234 (Not expected)
    Did you explain what was expected?
    Why we need to use the get and set methods when we can simply create any method such as "public int display()".....
    What is the purpose of having the display method?
    get and set are standard names for methods that are used to for the purpose that their names describe.

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    @ kian_hong2000

    Base on your example, at the second Example class(modified),
    That will obviously return "1234" if the given main method is still the same.....

    And logically, you are not expecting to return 1234.....
    IF you are expecting to show "2" value, then invoke first the setNo() method before capturing the returned value from the getNo() method.....
    freedom exists in the world of ideas

  6. #6
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    Maybe my questions are not clear.

    I want two result to shown the same (display "2" value) either i put the compare method in the set or get methods. But i am only able to get the "2" value if i put it inside get method not set method.

    I have seen some coding using for example display() without any set and get methods. I does not sure when to use set, get and none in front of the name of the methods.

    Do you mean that to invoke is to initialize inside the main method then it will show the "2 value"? Is there a way to show the set method to work once i put in the values to the contructor?


    public class TestExample{

    public static void main(String[] args){

    Example e = new Example("1234");
    e.setNo("2")
    System.out.println(e.getNo());
    }
    }
    Last edited by kian_hong2000; 08-27-2008 at 04:28 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    I want two result to shown the same (
    Sorry I still don't understand what you want to do.
    With the following code
    Example e = new Example("1234");
    e.setNo("1234")
    you appear to be settting the value of number two times: once in the constructor and once in the setNo method. Doing it once would be enough to have it set.

  8. #8
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    Oh...i put in the wrong value...

    Example 1: (The result give a "2" value if i use the setNo() to overwrites the 1234 in the contructor)

    Coding:
    public static void main(String[] args){

    Example e = new Example("1234");
    e.setNo("2")
    System.out.println(e.getNo());


    ************************************************** ******

    Is it possible if i put the value of 1234 to the contructor and it will pass the
    value to setNo() and return "2" value?

    Example 2:

    Coding
    public static void main(String[] args){

    Example e = new Example("1234");
    System.out.println(e.getNo());

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    i put the value of 1234 to the contructor and it will pass the value to setNo() and return "2" value?
    This makes no sense to me. Where would the value 2 come from?
    The method setNo() does NOT return a value. Normally methods named set.... set a value in the object, they do no return values.
    Java Code:
    Example e = new Example("1234");  // set value to 1234
    e.setNo("2"); // change value to 2
    System.out.println(e.getNo());  // should print 2
    
    Example e = new Example("1234");  // set value to 1234
    System.out.println(e.getNo());  // should print 1234

  10. #10
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    Ok...

    That means i have to set the method first before i can see the result in get method.

    Am i right to say constructor cannot contain set method?

    Previously, i thought if i put in the value to the constructor which have a set method inside, i can use get methods to get the "2" result back.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    constructor cannot contain set method?
    That is correct, constructors and methods cannot contain other methods. That is the code for the method cannot be inside of the code for another method. But methods can call each other.
    i have to set the method first before i can see the result in get method.
    Not sure what the above means. The constructor can save value that would be available for the get method.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 11:00 AM
  3. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 06:31 AM
  4. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:49 PM
  5. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM

Posting Permissions

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