THIS Keyword
What is the use of THIS keyword in a function. I have seen THIS being used in Javascript and Java. I went through few articles but could not understand fully. Whether in a function instead of using its object's name for each and every variable THIS is used?
THIS keywordWhat is the use of THIS keyword
Page 1 of 1
8 Replies - 1742 Views - Last Post: 30 December 2008 - 01:20 AM
Replies To: THIS keyword
#2
Re: THIS keyword
Posted 29 December 2008 - 07:43 AM
The keyword this is useful when you need to refer to instance of the class from its method. This keyword helps you avoid name conflicts in your program. Sometimes in your program, you may declare the name of instance variable and local variables with same names, thus you use "this" to avoid confliction. I hope that my clarification was helpful, good luck in you java
...
here is a sample program that demonstrates the use of "this":
here is a sample program that demonstrates the use of "this":
class Rectangle{
int length,width;
void show(int length,int width){
this.length=length;//The use of this
this.width=width;//The use of this
}
int calculate(){
return(length*width);
}
}
public class ShowRectangle{
public static void main(String[] args){
Rectangle rectangle=new Rectangle();
rectangle.show(5,6);
int areaRect = rectangle.calculate();
System.out.println("Area of Rectangle: " + areaRect);
}
}
This post has been edited by ayman_mastermind: 29 December 2008 - 07:52 AM
#3
Re: THIS keyword
Posted 29 December 2008 - 09:24 AM
ayman_mastermind, on 29 Dec, 2008 - 06:43 AM, said:
The keyword this is useful when you need to refer to instance of the class from its method. This keyword helps you avoid name conflicts in your program. Sometimes in your program, you may declare the name of instance variable and local variables with same names, thus you use "this" to avoid confliction. I hope that my clarification was helpful, good luck in you java
...
here is a sample program that demonstrates the use of "this":
here is a sample program that demonstrates the use of "this":
class Rectangle{
int length,width;
void show(int length,int width){
this.length=length;//The use of this
this.width=width;//The use of this
}
int calculate(){
return(length*width);
}
}
public class ShowRectangle{
public static void main(String[] args){
Rectangle rectangle=new Rectangle();
rectangle.show(5,6);
int areaRect = rectangle.calculate();
System.out.println("Area of Rectangle: " + areaRect);
}
}
Hi,
Here this.length refers to the local variable i.e refers to the variable of function show. Am i rite?
#4
Re: THIS keyword
Posted 29 December 2008 - 09:36 AM
Very simply the "this" keyword refers to "this class instance". So if I define a class called "myclass" and use the keyword "this" in one of its methods, this would point to itself "myclass". If you keep in mind "points to itself, this class instance" I think you will get the idea behind the this keyword.
In the class "myclass" this points to "myclass". In my other class instance called "myotherclass" the this keyword points to "myotherclass" etc.
In the class "myclass" this points to "myclass". In my other class instance called "myotherclass" the this keyword points to "myotherclass" etc.
#5
Re: THIS keyword
Posted 29 December 2008 - 09:47 AM
The keyword 'this' is used when refferring to a sub-class. For example, when creating a JFrame, you use the following code:
Now let's create a subclass of JFrame
As you can see, in the first class, we refer to the frame name while in the second class we use the 'this' keyword. This is because the second class is a JFrame and we are refferring to itself. Whenever you have a subclass and are calling methods from the super class (class after the 'extends keyword'), you use the 'this' keyword to invoke the methods from the super class. If you define methods in the subclass, call them normally.
import javax.swing.*;
public class JFramePractice{
public static void main(String[] args){
JFrame frame = new JFrame("Hello, World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(200,200);
}
}
Now let's create a subclass of JFrame
import javax.swing.*;
public class JFrameInherit extends JFrame{
public JFrameInherit(){
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Hello, Frame");
this.setVisible(true);
}
public static void main(String[] args){
new JFrameInherit();
}
}
As you can see, in the first class, we refer to the frame name while in the second class we use the 'this' keyword. This is because the second class is a JFrame and we are refferring to itself. Whenever you have a subclass and are calling methods from the super class (class after the 'extends keyword'), you use the 'this' keyword to invoke the methods from the super class. If you define methods in the subclass, call them normally.
#6
Re: THIS keyword
Posted 29 December 2008 - 09:54 AM
Again what macosxnerd101 is doing is pointing to the current instance. In his solution his this keyword is pointing to THIS instance which just happens to be a subclass of another class. It really has nothing to do with it being a subclass. The this keyword works in normal classes, parent classes, subclasses. It just so happens in that situation this refers to "this instance" of the class which was just a subclass.
It is a very subtle point and I want people to understand this. It has nothing to do with the inheritance itself.
It is a very subtle point and I want people to understand this. It has nothing to do with the inheritance itself.
#7
Re: THIS keyword
Posted 29 December 2008 - 09:51 PM
I'll add my touch
usually used for something like this
Now difficult to be clearer
class Person {
String name; // <--- we will call this guy name (1)
Person(String name) { // <--- we will call this guy name (2)
name = "abc"; // sets name (2) to "abc"
this.name = "abc"; // sets name (1) to "abc"
}
}
usually used for something like this
class Person {
String name;
Person(String name) {
this.name = name; // sets name (1) to the value of name (2)
}
}
Now difficult to be clearer
#8
Re: THIS keyword
Posted 29 December 2008 - 10:24 PM
Martyr2 made a very good point, one that I would like to expand on. Inheritance is one common use for the keyword 'this', but another use can be for instantiated objects. For this example, I will use an Integer object instead of a primitive.
As you can see, using 'this' is the same this as making a non-static call to a variable.
Integer x = this.MAX_VALUE; Integer y = y.MAX_VALUE;
As you can see, using 'this' is the same this as making a non-static call to a variable.
#9
Re: THIS keyword
Posted 30 December 2008 - 01:20 AM
Thank everyone here for taking your time to clear my doubts.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|