THIS keyword

What is the use of THIS keyword

Page 1 of 1

8 Replies - 2053 Views - Last Post: 30 December 2008 - 01:20 AM Rate Topic: -----

#1 av11453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-December 08

THIS keyword

Post icon  Posted 29 December 2008 - 07:26 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: THIS keyword

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

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":

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

Was This Post Helpful? 1
  • +
  • -

#3 av11453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-December 08

Re: THIS keyword

Posted 29 December 2008 - 09:24 AM

View Postayman_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":

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?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

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.

:)
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

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:

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.
Was This Post Helpful? 1
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

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.

:)
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: THIS keyword

Posted 29 December 2008 - 09:51 PM

I'll add my touch

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
Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

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.

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.
Was This Post Helpful? 0
  • +
  • -

#9 av11453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-December 08

Re: THIS keyword

Posted 30 December 2008 - 01:20 AM

Thank everyone here for taking your time to clear my doubts.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1