Results 1 to 6 of 6
  1. #1
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default .This and return / Getter and setter methods

    1. Hey guys I am trying to understand the concept of object oriented programming

    and right now I am working on Getter / setting methods


    Basically what I understand is, I can get an object from another class with the get method and then change the data with the set method. The point of this is so I could change the data, whenever I want instead of changing the original?

    Im not sure if thats right, if anyone could explain that would be awesome.


    2. .This and return

    I really cant understand .This, and returns.

    Example:

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

    public int getWeight() {
    return weight;
    }

    So the method "setName", what it is doing is saying "this name set it as name"? Is that just there so if I wanted to change it in the future I could?

    And then for the method "getWeight", returning is just like it says "returning the data and nothing else" but I dont know what that actually does.


    EDIT: Also for like this for example:

    public MyThisTest(int a) {
    this.a = a;
    }

    why not just do this:

    public MyThisTest(int a) {
    a = a;
    }

    ______________________


    Sorry, if this isnt much information for my questions. If you guys have a easy reference for me to read that would be great. Thank you.
    I just think I am over thinking this stuff and always makes me question it.
    Last edited by DayKema; 07-14-2014 at 06:37 PM.

  2. #2
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: .This and return / Getter and setter methods

    You seem pretty confused, so I'll try my best to explain everything to you.

    Now first I am going to correct you by saying that you don't just get objects from get methods, you can also get instance variables. Instance variables are declared in the beginning of your class, and are usually initialized(assigned values) in the constructor.
    When you use a get method, the point is to read the data for various purposes.
    When you use a set method, it is to change that instance variable's data.

    A return is basically what the value a method gives you. So let's say we are using an addition operator. 2+2. This expression would return a 4. You see where I'm going with this? Returning means the value you are going to get back from the method.

    The this. keyword is used to specify that you are referring to an instance variable of the current class. So check this out.
    Java Code:
    public MyThisTest(int a) {
    this.a = a;
    }
    You see when you call this method it would be MyThisTest(4). The int in the parenthesis(called the parameter) specifies the value type, and a is the name of the value that you will enter when you use this method. Now, we have two a variables. That can cause a problem. So this.a would refer to the variable of this class, and a would refer to the value that was entered when the method was used.

  3. #3
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Re: .This and return / Getter and setter methods

    Quote Originally Posted by coolman50544 View Post
    You seem pretty confused, so I'll try my best to explain everything to you.

    Now first I am going to correct you by saying that you don't just get objects from get methods, you can also get instance variables. Instance variables are declared in the beginning of your class, and are usually initialized(assigned values) in the constructor.
    When you use a get method, the point is to read the data for various purposes.
    When you use a set method, it is to change that instance variable's data.

    A return is basically what the value a method gives you. So let's say we are using an addition operator. 2+2. This expression would return a 4. You see where I'm going with this? Returning means the value you are going to get back from the method.

    The this. keyword is used to specify that you are referring to an instance variable of the current class. So check this out.
    Java Code:
    public MyThisTest(int a) {
    this.a = a;
    }
    You see when you call this method it would be MyThisTest(4). The int in the parenthesis(called the parameter) specifies the value type, and a is the name of the value that you will enter when you use this method. Now, we have two a variables. That can cause a problem. So this.a would refer to the variable of this class, and a would refer to the value that was entered when the method was used.
    Thanks alot. I appreciate you writing this all out to help me. I have a few questions though.

    So:

    public MyThisTest(int a) {
    this.a = a;
    }

    So: "this.a = a; the this.a is referring to the instanced variable "a" and not the new a? Okay. Correct me if I am wrong.


    _________________________

    Okay and a new thing came up.
    Look here:

    I wrote this:

    public int setHour(int h){
    hour = ((h >= 0 && h < 24) ? h : 0);
    }

    and now it will tell if the variable is true of false basically, I have to use a get method to receive the variable?

    like this:?

    public void getHour(){
    return hour;
    }

    Why do I have to do this? wouldnt the set method already give me the answer if it is true or false.

    Thanks alot for the help by the way.
    Last edited by DayKema; 07-14-2014 at 07:30 PM.

  4. #4
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: .This and return / Getter and setter methods

    Quote Originally Posted by DayKema View Post
    Thanks alot. I appreciate you writing this all out to help me. I have a few questions though.

    So:

    public MyThisTest(int a) {
    this.a = a;
    }

    So: "this.a = a; the this.a is referring to the instanced variable "a" and not the new a? Okay. Correct me if I am wrong.


    _________________________

    Okay and a new thing came up.
    Look here:

    I wrote this:

    public void setHour(int h){
    hour = ((h >= 0 && h < 24) ? h : 0);
    }

    and now it will tell if the variable is true of false basically, I have to use a get method to receive the variable?

    like this:?

    public void getHour(){
    return hour;
    }

    Why do I have to do this? wouldnt the set method already give me the answer if it is true or false.

    Thanks alot for the help by the way.

    EDIT: In netbeans I get a note thing saying its unnecessary to have the return in the getMethod.
    That's right. The this.a is the instance variable and the a in the parameter is the new a.

    The second thing you did it wrong, though. If you are to test some conditions on a value, and you want a true or false value you'd have to do this.

    Java Code:
    public void checkHour(int hour) {
       if(hour >= 0 && h < 24)
          return true;
       else
          return false;
    }
    I changed the name, because that is more appropriate. What's gonna happen here is that the if statement is going to check if it is greater then or equal to 0 and less than 24. If so, then it returns a true value. If not, then it returns a false value. Was this what you were trying to do?

  5. #5
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Re: .This and return / Getter and setter methods

    No I was passing in a variable from another class, and testing it to see if it was greater than 0 or less than 24, if it was it would use the variable passed in, otherwise use 0.

    Ill give you all my code:

    Java Code:
    public Tuna(){
        this(0,0,0);
    }
    public Tuna(int h){
        this(h,0,0);
    }
    public Tuna(int h, int m) {
        this(h,m,0);
    }
    public Tuna(int h, int m, int s) {
        setTime(h,m,s);
    }
    
    public void setTime(int h,int m,int s){
        setHour(h);
        setMinute(m);
        setSecond(s);
    }
    
    public void setHour(int h){
    hour = ((h >= 0 && h < 24) ? h : 0);
    }
    public void setMinute(int m){
        minute = ((m >= 0 && m <= 60) ? m : 0);
    }
    public void setSecond(int s){
        second = ((s >= 0 && s <= 60) ? s : 0);
    }
    
    public int getHour(){
        return hour;
    }
    public int getMinute(){
        return minute;
    }
    public int getSecond(){
        return second;
    }
    
    public String toMilitary(){
        return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
    }

    These are from buckys tutorials if you are familiar.

    I am just learning how to pass in a variable from another class, if thats the correct usage of terms
    Last edited by DayKema; 07-14-2014 at 07:51 PM.

  6. #6
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: .This and return / Getter and setter methods

    Quote Originally Posted by DayKema View Post
    No I was passing in a variable from another class, and testing it to see if it was greater than 0 or less than 24, if it was it would use the variable passed in, otherwise use 0.

    Ill give you all my code:

    Java Code:
    public Tuna(){
        this(0,0,0);
    }
    public Tuna(int h){
        this(h,0,0);
    }
    public Tuna(int h, int m) {
        this(h,m,0);
    }
    public Tuna(int h, int m, int s) {
        setTime(h,m,s);
    }
    
    public void setTime(int h,int m,int s){
        setHour(h);
        setMinute(m);
        setSecond(s);
    }
    
    public void setHour(int h){
    hour = ((h >= 0 && h < 24) ? h : 0);
    }
    public void setMinute(int m){
        minute = ((m >= 0 && m <= 60) ? m : 0);
    }
    public void setSecond(int s){
        second = ((s >= 0 && s <= 60) ? s : 0);
    }
    
    public int getHour(){
        return hour;
    }
    public int getMinute(){
        return minute;
    }
    public int getSecond(){
        return second;
    }
    
    public String toMilitary(){
        return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
    }

    These are from buckys tutorials if you are familiar.

    I am just learning how to pass in a variable from another class, if thats the correct usage of terms
    I see, well you would still use the if statement and then do whatever you want with them.

Similar Threads

  1. setter and getter
    By rajeev in forum New To Java
    Replies: 2
    Last Post: 08-18-2013, 02:59 AM
  2. Call remote getter/setter
    By rcbandit in forum Advanced Java
    Replies: 1
    Last Post: 06-26-2013, 03:46 PM
  3. help with getter and setter methods of this question.
    By luciawashaya in forum New To Java
    Replies: 6
    Last Post: 11-01-2011, 11:28 AM
  4. getter and setter method help please!
    By merdzins in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 05:06 AM
  5. use of private in getter and setter
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 07:17 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
  •