Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

help me to understand "THIS" key word

  Asked By: Danielle    Date: Jun 01    Category: Java    Views: 1036
  

i am very much bignner to the world of java, i am unclear about the usage of the
key word "THIS" in java, can one help me! , especially about its usage in the
functions.

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Utsav Shah     Answered On: Jun 01

The this keyword is basicly used to refference to the particular
instance of a Object.

The easiest example is variable names:

If you had a global variable Object called Bob

Object bob;

and you had a method that had accepted a Object in it's method signiture

public void methodName(Object bob){...}

You can do something like this

public void methodName(Object bob){
this.bob = bob;
}

 
Answer #2    Answered By: Ziza Mizrachi     Answered On: Jun 01

let me illustrate one more example which may make the concept of "this" clear to
u.

Let's take the following peice of code:

public void addFirst)(Object item)
{
head = new ListNode(item,node);
++size;
}


ListNode(Object data ,ListNode next)
{
this.data = data;
this.next = next;

}

Notice here that we have a simple bit of a program that creates a linked list.
What u see in the constructor ListNode is interesting.\

Let's go back to the following line of code:
head = new ListNode(item,node);

which has item and node as parameters. Actually these are references and are
REFERENCE VARIABLES in java.

These are passed to Object data ,ListNode next - the intermediate reference
variables that take on the references from the item and node respectivelly

Now let us go back to the Class ListNode which is as follows:

class ListNode{

Object data;

List Node next;
ListNode(Object data ,ListNode next)
{
this.data = data;
this.next = next;

}

which is the same i wrote earlier
.Now notice that the INSTANCE VARIABLES DECLARED IN THE class ListNode are
Object data and ListNode next.

These are similar in names to the reference variable, which u will readily
agree.

Now to distinguish between these 2 we have to write statements like
this.data = data;
//here this.data refers to Instance variable "data" declared in ListNode and
this.next agains refers to instance variable "next" declared in ListNode

Since we are passing the reference from reference variables "data" and "next" to
Instance Variables "data" and "next" the distinguishing syntax is necessary to
avoid getting the compiler to give u error messages and getting confused

So are u with me upto this? If u have any doubts mail me again and i will be
glad to try to sort out any question u may have.

 
Answer #3    Answered By: Fairuzah Alam     Answered On: Jun 01

Please refer to the book "A Programmer's Guide to Java Certification" by Khalid
Mughal and published by Addison Wilsey,in this "this" keyword is taught in a
very good way.

 
Answer #4    Answered By: Gerardo Morgan     Answered On: Jun 01

"This" can be explained very simply. It is simply a reference to the
particular instance of an object you call a method on. When you call
a methjod, for example:

lemon.squeeze();

the "this" pointer is passed as an invisible argument - the memory
address of the particular object instance, so the operation is
performed on the correct object - and this is why static methods do
not pass the "this" pointer as they sit outside the object hierarchy -
which is why you get those error messages when you try and refer to
instance data from a static block.

"This" is also used in an idiosyncratic way inside constructors.

 
Answer #5    Answered By: Kawakib Mansour     Answered On: Jun 01

I would refer you the book "A Programmer's Guide to Java Certification" by
Khalid A. Mughal and Rolf W.Rasmussen published by Addison Wsley,in this book
the author has completely explained the usage  of "this" key  word in a very good
manner ,it is one of the best Java books I have ever read, try to find this
book,if you still face problem than just mail me I will tell you the complete
usage.

 
Didn't find what you were looking for? Find more on help me to understand "THIS" key word Or get search suggestion and latest updates.




Tagged: