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: Still don't understand what the 'this' keyword does

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Still don't understand what the 'this' keyword does

    For the life of me I'm confused about the purpose of the 'this' keyword. I've read the bit in "Head First Java" but I still don't get it. What does it do, and why would anyone want to use it. The few pages or so in the book just don't make sense to me. Can someone offer a clear(er) explanation?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Still don't understand what the 'this' keyword does

    The this keyword is a reference to the instance of a class where code is being executed from within a method in that class.
    Normally it is not required to code this as the compiler assumes its presence.
    The one time it is needed is when a variable is declared within a method with the same name as a variable declared at the class level.
    public class TestThis {
      int aVar;           // class level
      private void aMethod(int aVar) {   //  Argument with same name
         this.aVar = aVar;    // copy arg to class instance variable
      }
      public static void main(String[] args) {
         TestThis tt = new TestThis();
         tt.aMethod(12);
         System.out.println("aVar="+ tt.aVar);
      }
    }
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Location
    Noida
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Still don't understand what the 'this' keyword does

    'This' keyword is used to refer instance variable of current class or you can say, it returns the current class instance. It invokes the current class constructor/method. It pass an argument in method/constructor call. We use 'this' keyword to remove the confusion between the class attributes and parameters with the same name.

  4. #4
    Junior Member
    Join Date
    Jun 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Still don't understand what the 'this' keyword does

    There are many uses of the Java "this" keyword. In Java, the "this" keyword is the reference variable that refers to the current object.

    Usage of Java "this" keyword, 6 usages of java this keyword.

    1.) the "this" keyword can be used to refer to the current class instance variable.
    2.) the "this" keyword can be used to invoke the current class method implicitly.
    3.) the "this" keyword can be used to invoke a current class constructor.
    4.) the "this" keyword can be used to pass as an argument in the method call.
    5.) the "this" keyword can be used to pass as an argument in the constructor call.
    6.) the "this" keyword can be used to return the current class instance from a method.

    If you want to understand the working of 'this' keyword using program then you can mention it in replying.

Similar Threads

  1. this. Keyword
    By Jnic in forum Java Theory & Questions
    Replies: 5
    Last Post: May 31st, 2013, 02:29 AM
  2. Trying to understand the "protected" keyword.
    By simpson_121919 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 30th, 2013, 08:57 PM
  3. Uses of throws keyword
    By vilkas in forum Exceptions
    Replies: 1
    Last Post: December 11th, 2012, 03:17 AM
  4. [SOLVED] The 'new' Keyword
    By TheSlowLearner in forum Object Oriented Programming
    Replies: 17
    Last Post: August 7th, 2012, 12:05 AM
  5. keyword Extends
    By chronoz13 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2009, 07:30 AM