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: What is the use of "this." and implicit parameters?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default What is the use of "this." and implicit parameters?

    While reading my Java book, I am trying to understand exactly what is the use of the implicit parameters and the this. .
    The book says that s has something to do with implicit parameters but I can't seem to get the grasp of it. Could anyone give a little explanation?

    Thanks in advance.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: What is the use of "this." and implicit parameters?

    I feel that I understand the uses of *this* in Java programs, but for the life of me find it to be one of the hardest concepts to discuss with other people, that it is perhaps more abstract than other Java concepts,... but despite that, here goes another attempt.

    Inside of your class, *this* refers to the current object of the class the one whose code is currently being run. For instance, say you have a class called Foo:

    public class Foo {
      private int bar;
     
      public Foo(int bar) {
        this.bar = bar;  // line (A)
      }
      public void someMethod() {
         System.out.println("bar is " + this.bar); // line (B)
      }
    }

    if later you use this class to create a few objects:

    public class Baz {
      public static void main(String[] args) {
        Foo foo1 = new Foo(3);  // line (C)
        Foo foo2 = new Foo(5);  // line (D)
     
        foo1.someMethod();  // line (E)
        foo2.someMethod();  // line (F)
      }
    }

    On line (C) you create a new Foo object and have foo1 refer to it. When the constructor is called the *this* inside of the constructor on line (A) will refer to the current object being constructed, foo1. So the bar variable being dereferenced on this object will be foo1's bar variable. On the other hand when line (D) is reached, *this* will refer to the foo2 Foo object that is being constructed, and the this.bar variable will be foo2's bar. So while it's the same Foo class for both, but the *this* only refers to the current object that controls the class's code at that time (understand that foo1 and foo2 share the exact same code).

    Likewise on line (E), when foo1 calls someMethod, the *this* inside of someMethod on line (B) refers to the foo1 object, and will return 3, while on line (F), the *this* in this method will refer to foo2 and will return 5.

    Note that since the Foo constructor's parameter, bar, and the class field, bar, have the same name, the *this* in the constructor on line (A) is required so the compiler will know which bar you're mean to refer to, the bar variable defined in the constructor's parameter (which gets no *this* before it), or the bar variable declared in the class (which requires the *this* in front of it). If you gave the parameter a different variable name, then no this. would be needed since it would be clear to the compiler which variable you mean to use always.

    Note that the *this* used in line (B) is not needed since it is already "implicitly" present when using the variables of the class.

    Clear as Mudd?
    Last edited by curmudgeon; September 23rd, 2012 at 12:50 AM.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    jean28 (September 23rd, 2012)

  4. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: What is the use of "this." and implicit parameters?

    Some clarification of the above, again this bit of code:

      public void someMethod() {
         System.out.println("bar is " + this.bar); // line (B)
      }

    is equivalent to
      public void someMethod() {
         System.out.println("bar is " + bar); // line (B)
      }

    Since there is no confusion as to which *bar* this is since the method has no parameter or local variable by the same name, then the *this.* part is not required as it is implicitly present and does not need to be explicitly written.

    Also, other uses for *this*:
    • In a constructor, *this* can allow the constructor to call other constructor overloads of the same class. e.g.
      public class Foo {
        private int i;
        private String text;
        public Foo(int i) {
          this.i = i;
          text = "";
        }
       
        public Foo(int i, String text){
          this(i); // *** here!!! ***
          this.text = text;
        }
      }
    • The identifier *this* can be used when trying to clarify which object you're referring to when you are inside a non-static inner class. i.e., MyOuterClass.this.someMethodOfOuterClass()
    • .... and I'm sure that there's more that I've forgotten, but hopefully the forum gurus will fill the rest in!
    Last edited by curmudgeon; September 23rd, 2012 at 09:23 AM.

  5. #4
    Junior Member
    Join Date
    Dec 2012
    Location
    United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What is the use of "this." and implicit parameters?

    Implicit parameters are implemented as described in Implicit parameters: dynamic scoping with static types, J Lewis, MB Shields, E Meijer, J Launchbury, 27th ACM Symposium on Principles of Programming Languages (POPL'00), Boston, Jan 2000. Note however that the binding syntax in that paper, using keywords dlet and with, has been replaced by the form presented below.

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Why the error said "few parameters expected 7" while i only have 6 fields?
    By Hafiz Mughni in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 9th, 2011, 10:45 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM