Results 1 to 3 of 3

Thread: A small query

  1. #1
    David1234204 is offline Member
    Join Date
    Mar 2012
    Location
    London
    Posts
    7
    Rep Power
    0

    Default A small query

    I've seen many code samples on both this forum and on other webpages which use the "this" keyword when calling methods that have been inherited. Is this really necessary?

    Here's a code example:

    Java Code:
    public class Example extends JFrame { 
    
    	public Example() {
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Both compile and work but does the "this" keyword make any difference?
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}	
    }
    Just a small question which I'm a little perplexed about...
    Last edited by David1234204; 03-21-2012 at 10:26 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: A small query

    The this keyword refers to the object you are in. As an example, imagine you have the following scenario:

    Java Code:
    public class Example{
      private int x;
      public Example(int x){
        x = x;
      }
    }
    Now, in the constructor, what should happen? Are you assigning the instance variable to the passed in value, or vice versa? This is ambiguous, which you can't have. You can obviously change the variable name in the constructor, but why create another variable name when you can use an existing one?

    In the snippet you showed either work, but without the this, ambiguities may arise. It's easier to add this to explicitly show what you are doing.

    If this doesn't clear everything up, let me know. Here is the java tutorials as well: Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    You will often see something like

    Java Code:
    addActionListener(this);
    Which is generally used when both the object if an action listener and it is something which is getting an action listener attached to it. It explicitly refers to the current object you are working on. When an instance of the object is created the this keyword is referring to that instance explicitly.
    Last edited by sunde887; 03-21-2012 at 11:07 PM.

  3. #3
    David1234204 is offline Member
    Join Date
    Mar 2012
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Re: A small query

    Quote Originally Posted by sunde887 View Post
    In the snippet you showed either work, but without the this, ambiguities may arise. It's easier to add this to explicitly show what you are doing.

    When an instance of the object is created the this keyword is referring to that instance explicitly.
    Thanks for your reply, this has answered my question perfectly! :)

Similar Threads

  1. Small JAR problem
    By Kaizer in forum New To Java
    Replies: 11
    Last Post: 12-16-2011, 07:16 PM
  2. Small game
    By ivlatt in forum Java Applets
    Replies: 5
    Last Post: 04-12-2011, 02:15 PM
  3. small windows
    By ghostofdarling in forum Java Applets
    Replies: 1
    Last Post: 04-17-2010, 08:55 AM
  4. small problem
    By rawan in forum Java Applets
    Replies: 2
    Last Post: 04-07-2010, 03:32 AM
  5. small problem
    By barusk in forum Networking
    Replies: 4
    Last Post: 03-21-2009, 06:19 AM

Posting Permissions

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