Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: 'this' implicit parameter

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 'this' implicit parameter

    Good evening everybody!

    I'm learning Java right now in university and, for the most part, it's making sense. I'm struggling to understand how the implicit parameter 'this' works. Take, for example, the following block of code:

    public class ShapeSquare {
    // private fields
    private double sideLength;
     
    // public methods
    public void setSideLength(double sideLength) {
    this.sideLength = sideLength;
    }
     
    public double getArea() {
    return sideLength * sideLength;
    }
    }

    I'm getting more and more familiar with language, so I don't need it to be in layperson's terms, but the simpler you can make the explanation the better. Thanks in advance everyone! You'll probably hear from me lots as I continue to learn!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 'this' implicit parameter

    Can you be more specific about the problems you are having?

    Is your question about this statement:
    this.sideLength = sideLength;

    The variable sideLength is defined in two separate scopes: one in the class and one in the method.
    this is needed to reference the one in the class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 'this' implicit parameter

    Quote Originally Posted by Norm View Post
    Can you be more specific about the problems you are having?

    Is your question about this statement:
    this.sideLength = sideLength;

    The variable sideLength is defined in two separate scopes: one in the class and one in the method.
    this is needed to reference the one in the class.
    Hey - yes this was exactly my question. So essentially, the
     this
    line is equating the SideLength variable in the Class with the SideLength in the method?

    If this is right, thanks so much!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 'this' implicit parameter

    A description of that statement would be: assign the value of the local variable to the class level variable
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: February 9th, 2019, 12:48 PM
  2. Objects as parameters, and implicit calling?i
    By MW130 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 16th, 2013, 05:08 PM
  3. Replies: 3
    Last Post: May 27th, 2012, 11:11 AM
  4. Junit3 error: Implicit super constructor TestCase() is not visible
    By albertkao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 12:53 PM
  5. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 05:04 PM