English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

can anyone explain what it does, maybe with a simple example? It has always confused me!
thanks

2007-03-19 03:04:15 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

The "this" keyword points to the class in which it appears. By using "this", you refer to the object itself which is going to be instantiated from this class.

If you have a setter method or a constructor, the "this" keyword can be used to refer to the attribute of the class itself, such as:

/*attributes are private, public methods are used to put and
*get values to and from the private attributes
*/
private int value;

public void setValue(int value)
{
this.value = value;
}

What happened in this code. There are two variables that have the name "value": one in the class itself, which is being reffered to with "this.value" and one in the arguments of the "setValue" method. If we put there "value = value", nothing would happen, because the value within the function "setValue" would have gotten its own value. Here the "this" keyword points to the fact that the private attribute "value" declared in the class gets the value of the argument "value" of the "setValue" method.

In some other cases "this" can be handy as well. If you have to call a function with an argument containing this class, then you use the keyword "this":

public class Example
{
public Example(){}
public void doSomething()
{
B b = new B(this);
}
}

public class B
{
private Example example;

public B(Example example)
{
this.example = example;
}
}

Here an Example object is required in order to construct a B object. Hence, in the constructor of the Example class we construct a B object with as argument the Example class itself. Hence, if we create an Example object, a B object will be created:

Example example = new Example();

2007-03-19 03:13:46 · answer #1 · answered by stevevil0 3 · 0 0

The "this" keyword refers to the object itself, as opposed to its methods.

Let's say you are creating a class called PuzzleSolver, and the PuzzleSolver has a variable called 'score.' You might also have a constructor or a method that takes a parameter that is also called 'score.' At this point you have two different variables that both have the same name, but the method's 'score' variable can only be used in the method itself. If you want to use the PuzzleSolver's 'score' variable in the method, you can say this.score = whatever.

So 'this.score' means "the PuzzleSolver's 'score' variable, not the local method variable with the same name.

Hope this helps!
.

2007-03-19 10:11:33 · answer #2 · answered by Independent 2 · 1 0

'this' is a pointer pointing to your class object/s you created.
You create an object using your own class or using a pre-defined classes. Now 'this' keyword actually stores the address of the object you created in some place in memory.
You may say that there is no pointer in Java. But internally at some place we need to store address of certain variables, object etc so that we can use later or refer it at some other place. It is same as pointer in C/C++.

2007-03-19 10:16:01 · answer #3 · answered by Mr.X 1 · 0 0

If you class or name 2 variables with the same name, then the 'this' keyword variable would come as being the closest one if you search for that variable.

or something like that...

2007-03-19 10:08:44 · answer #4 · answered by AnarchyAlchemy 3 · 0 2

vido scriped it allows you to go to and yous animeded sites and vidio stuff

2007-03-19 10:11:48 · answer #5 · answered by Sakura *** 1 · 0 1

fedest.com, questions and answers