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 2 of 2

Thread: Why do we use the "this" keyword?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why do we use the "this" keyword?

    Hi guys,

    I am studying for a test, and one of sections is on inheritance.

    And I really don't understand how the "this" keyword works.

    From what I have read, this just references the active variable/object.

    So for example, why is "this" used in the below code?

    Such as with this.pages = pages;
    and with this.definitions = definitions;

    /**
     
     * Words.java - Demonstrates the use of the super reference..
     
     *
     
     * @author Lewis/Loftus
     
     * @version 2nd ed.
     
     */
     
    public class Words2
     
    {
     
       //-----------------------------------------------------------------
     
       //  Instantiates a derived class and invokes its inherited and
     
       //  local methods.
     
       //-----------------------------------------------------------------
     
       public static void main ( String[] args )
     
       {
     
          Dictionary2 webster = new Dictionary2( 1500, 52500 );
     
     
     
          webster.pageMessage();
     
          webster.definitionMessage();
     
       }
     
    }

    /**
     
     * Book.java - represents a book.
     
     *
     
     * @author Lewis/Loftus
     
     * @version 2nd ed.
     
     */
     
    public class Book2
     
    {
     
    	protected int pages;
     
     
     
    	/**
     
    	 * constructor
     
    	 *
     
    	 * @param int (pages)
     
    	 */
     
    	public Book2( int pages )
     
    	{
     
    		this.pages = pages;
     
     
     
    	} // constructor
     
     
     
    	/**
     
    	 * pageMessage - prints a message concerning the pages of this book.
     
    	 */
     
    	public void pageMessage()
     
    	{
     
    		System.out.println( "Number of pages: " + pages );
     
     
     
    	} // method pageMessage
     
     
     
    } // class Book

    /**
     
     * Dictionary2.java - represents a dictionary, which is a book
     
     *
     
     * @author Lewis/Loftus
     
     * @version 2nd ed.
     
     */
     
    public class Dictionary2 extends Book2
     
    {
     
    	private int definitions;
     
     
     
    	/**
     
    	 * constructor
     
    	 *
     
    	 * @param int (pages)
     
    	 * @param int (definitions)
     
    	 */
     
    	public Dictionary2( int pages, int definitions )
     
    	{
     
    		super( pages );
     
     
     
    		this.definitions = definitions;
     
     
     
    	} // constructor
     
     
     
    	/**
     
    	 * definitionMessage - prints a message using both local and inherited
     
    	 * values
     
    	 */
     
    	public void definitionMessage()
     
    	{
     
    		System.out.println("Number of definitions: " + definitions);
     
    		System.out.println("Definitions per page: " + definitions/pages);
     
     
     
    	} // method definitionMessage
     
     
     
    } // class Dictionary (extends Book2)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why do we use the "this" keyword?

    this references the 'current' object (see Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)). It is useful in many situations, one of which is in the code you posted. For example, imagine the following scenario
    public Dictionary2( int pages, int definitions )
    	{
    		super( pages );
    		definitions = definitions;//bad code design
    	} // constructor
    In the above example, the parameter definitions will be set to itself (a pretty useless line of code) while the object variable definitions never gets initialized as intended

Similar Threads

  1. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  2. Replies: 1
    Last Post: March 15th, 2010, 10:03 PM
  3. "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
  4. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM