5 Replies - 1073 Views - Last Post: 04 February 2009 - 07:43 AM Rate Topic: -----

#1 slikz  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 03-February 09

The proper use of the "this" keyword

Posted 03 February 2009 - 07:05 PM

Hello everyone. I'm having a little trouble figuring out the proper use for the "this" keyword. I have read this thread, this thread and this thread as well as the Sun documentation, but I'm still struggling to grasp it's use.

I am in an introductory programming course and have a few months of self teaching under my belt as well, but I have yet to come across a problem where I have had to use "this." Today my teacher told our class to ALWAYS use "this" for accessing instance variables, but all of the programming I have ever done I've never used "this" to access my instance variables. Furthermore, one of the books I have says that putting "this" in front of every instance variable (no matter what) is somewhat of a sin.

For example, here is my finished code for my assignment today:

public class MyApp {
	
	public static void main(String[] args) {
		
		Student myStudent = new Student(101, "Tyler");
		myStudent.setGrade(86.0);
		myStudent.printInfo();
		
		Student otherStudent = new Student(102, "Tom");
		otherStudent.setGrade(71.0);
		otherStudent.printInfo();
		
	} //end of main
} //end of class MyApp


public class Student {
	
	private String name;
	private String grade;
	private int id;
	public static final int maxCourses = 5;
	
	public Student() {
	}
	
	public Student(int id) {
		this.id = id;
	}
	
	public Student(int id, String name) {
		this.name = name;
		this.id = id;
	}
	
	public String getName() {
		return this.name;
	} 
	
	public int getId() {
		return this.id;
	}
	
	public String getGrade() {
		return this.grade;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public void setGrade(double mark) {
		
		if (mark < 50.0) {
			this.grade = "F";
		} else if (mark < 70.0) {
			this.grade = "C";
		} else if (mark < 85.0) {
			this.grade = "B";
		} else {
			this.grade = "A";
		}
	}
	
	public void printInfo() {
		
		System.out.println("Name: " + this.name);
		System.out.println("ID: " + this.id);
		System.out.println("Grade: " + this.grade);
		System.out.println("Max Courses: " + Student.maxCourses + "\n");
	}
} //end of Class Student


It is my gut reaction that this isn't a good use of the "this" keyword. Am I right in assuming that I could eliminate every "this" in the Student class simply by changing the parameter names to something other that the instance variable names? Why use it in the get and set methods? It also seems pointless to use the "this" keyword when printing the variables because different object references are invoking the print method, and therefore already know what instance variables to print.

For example:

public Student(int anId) {
		id = anId;
	}


So I guess what I am trying to say is why use "this" everywhere; why not just change the parameter name? Is it a matter of taste or convention to use one over the other?

To conclude, can anyone show examples where the "this" keyword is properly used; something more complex that simply getters and setters? If there are any threads I may have missed in my search, a point in the right direction would be appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: The proper use of the "this" keyword

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: The proper use of the "this" keyword

Posted 03 February 2009 - 07:10 PM

It's a matter of personal preference normally. I use it when I assign the instance variables values simply because it helps me distinguish different parts of code from others. It tells me that "OK, this is an instance variable. So don't think it's a local one".
Was This Post Helpful? 1

#3 pbl  Icon User is offline

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

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

Re: The proper use of the "this" keyword

Posted 03 February 2009 - 07:38 PM

View PostLocke, on 3 Feb, 2009 - 06:10 PM, said:

It's a matter of personal preference normally. I use it when I assign the instance variables values simply because it helps me distinguish different parts of code from others. It tells me that "OK, this is an instance variable. So don't think it's a local one".

Locke is right (as most of the time :D )
but if you use Javadoc, it is great to have the parameters match the instance variable name so in the doc you have for

int abcd;

void setWhatever(int abcd) {
   this.abcd = abcd;
}



setWhatever @param abcd
Was This Post Helpful? 1

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: The proper use of the "this" keyword

Posted 04 February 2009 - 05:59 AM

View Postslikz, on 3 Feb, 2009 - 08:05 PM, said:

So I guess what I am trying to say is why use "this" everywhere; why not just change the parameter name?


Because you can. ;) There are two optional things I always do. One is braces even when I don't need them. The other is "this", basically for the same reason,

Using "this" makes it absolutely clear where that variable you're referencing came from. It doesn't really matter in a small method, but if you're a few lines down it's nice to know the thing is object level and not method level without hunting about for it.

In the end, it is just style, though.
Was This Post Helpful? 1

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: The proper use of the "this" keyword

Posted 04 February 2009 - 07:31 AM

Although I would argue that it is necessary sometimes:

class myPanel extends JPanel {

private JFrame frame;

public myPanel()
{
   frame = new JFrame(640, 480);
  frame.add(this); //there's not really an alternative here
}
}



And if you ever switch to C/C++, the use of this differentiates depending on the context.
Was This Post Helpful? 1

#6 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: The proper use of the "this" keyword

Posted 04 February 2009 - 07:43 AM

I use this usually only when the parameter passed matches the instance variable name, as well as sometimes calling overloaded instructors, or cases like KYA said.

Though sometimes I use this because I am too lazy to type the class method/paramter name (Eclipse can auto fill in for you when you use this and special cases/circumstances or you can use shortcuts)
Was This Post Helpful? 1

Page 1 of 1