Results 1 to 4 of 4
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Usageof this keyword !

    Suppose I have the following code for Linked List:

    class Node{

    int data;

    Node next = null;

    public Node(int d){

    data = d;
    }

    void append(int d){

    Node end = new Node(d);
    Node n = this;

    while(n.next ! = null){
    n = n.next;
    }
    n.next = end;
    }
    }

    // What does "Node n = this " means in the above code snippet? What does "this" keyword signifies there? Please explain. I'm confused.
    Thanks in advance.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default Re: Usageof this keyword !

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,007
    Rep Power
    3

    Default Re: Usageof this keyword !

    This always references the current object: Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Node n = this means: Node n is assigned a value, being the current node, i.e. the Node object that append() is called on. In the loop following that, n is assigned a new value, being the next node.

  4. #4
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Usageof this keyword !

    Thanks everyone for your answer !!

    Appreciated !!

Similar Threads

  1. The this keyword
    By Army in forum New To Java
    Replies: 15
    Last Post: 12-14-2012, 05:01 AM
  2. Using 'this' keyword in constructor
    By kudwn in forum New To Java
    Replies: 2
    Last Post: 10-11-2010, 01:12 PM
  3. Use of keyword instanceof
    By darek9576 in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 11:35 PM
  4. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 10:20 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 08:32 PM

Posting Permissions

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