11 Replies - 627 Views - Last Post: 23 July 2011 - 11:50 AM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 03:31 PM

What is the difference in the codes below:

public Person(String firstName, String lastName){
        this.fn=firstName;
        this.ln=lastName;                
    }


_____And______

public Person(String firstName, String lastName){
        fn=firstName;
        ln=lastName;                
    }

Is This A Good Question/Topic? 0
  • +

Replies To: What is "this" statement? Can somebody give me an easy example

#2 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 03:41 PM

Well, assuming Person is defined (roughly) like this:

class Person
{
   private String fn, ln;

   public Person(String firstName, String lastName)
   {
      // either of the above code
   }
}



Then they're the same. When you instantiate a class, you're given an object. For example:

Person p = new Person("John", "Doe");
Person p2 = new Person("Foo", "Bar");



The "this" keyword comes into it like this: this refers to the object being referenced, is the best I can put it. From inside object p, "this" would refer to object p. "this" inside object p2 would refer to p2, if that makes sense.

Because the compiler is smart, doing "this.fn = ..." is the same as "fn = ...". However, if you named variables in a method the same as in the class, then sometimes you might get funny business. Very rarely though.

Does that make sense?

Edit: I thought it worth adding. If you changed the instance members to be public instead of private, then you'd notice the following things (if it helps make "this" easier to understand):

Calling p.fn from outside the object would give the same data as calling this.fn from inside the object.

This post has been edited by Hiram: 21 July 2011 - 03:43 PM

Was This Post Helpful? 1
  • +
  • -

#3 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 03:42 PM

The this keyword simply means inside the current object. Like let's say you had a method and it wanted a copy of the current class as an argument, you would pass it the this keyword to mean the current class. One example slightly like the code you have shown above, yet more practical because the two code snippets you have shown above do the exact same thing:
String firstName = ""; //Initialize the variables
String lastName = "";

public Person(String firstName, String lastName){
    this.firstName = firstName; //the this.firstName refers to the variable initialized in the first line the just firstName means the variable passed as an argument
    this.lastName = lastName; //same thing as above
}


So, in short, this simply means the current object, i.e. the object that the current code is being executed in.
Was This Post Helpful? 1
  • +
  • -

#4 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 03:43 PM

In that particular case there isn't one. but say you declared the name variables then you would have to specify which was which.

private String firstName;
private String lastName;

public Person(String firstName, String lastName){
    this.firstName = firstName; //first name in this case refers to the constructors first name
//this.firstName referes to your private ?global? variable firstName
    this.lastName = lastname; //same deal
}


Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 03:45 PM

You really mean

public Person(String firstName, String lastName) {
	this.firstName  =  firstName;
	this.lastName =  lastName;
}


'this' is the implicit reference to the instance and is here used to distinguish the instance variable from the parameter.
You should follow this pattern in all your ctors

This post has been edited by g00se: 21 July 2011 - 03:47 PM

Was This Post Helpful? 1
  • +
  • -

#6 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 03:52 PM

Ohhh, okay. Cool i get it. Then why not have this everytime i code? :bananaman:

Btw
Damn! you guys are quick, i just posted it like 3 min ago. And i already have 3 responses. I was about to leave my house in order get buy groceries hoping that by the time i get back i'd at least have one answer. I guess you guys dont want me to eat. jk

Thanks Everybody!! :D
Was This Post Helpful? 0
  • +
  • -

#7 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 04:01 PM

We're pretty quick :P

Some people think that not having "this" might make your code easier to look at (i.e. no walls of "this"). However it does sacrifice clarity, in that you might have to scroll back up a lot to find out which variables belong to the object, and which are local variables.

Personally I prefer to put this there explicitly. That way you know you're referring to an instance member, not a local variable.

Sometimes though, having the same named variables for a method and for a class could produce unexpected results, for example:

class Person
{
   private String firstName, lastName;

   public Person(String firstName, lastName)
   {
      firstName = firstName;
      lastName = lastName;
   }
}



In the constructor, which variable is being assigned? The one defined as an instance member, or the arguments to the constructor? Usually Java would do a pretty good job at picking which one you meant, but not always. You can nest variable names in different scope levels too, and make it even more confusing :D
Was This Post Helpful? 0
  • +
  • -

#8 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 04:11 PM

True! Yes, I remember having this code and asking myself why they have same variable names, as if it was just yesterday....wait! actually it was yesterday.

Sigh!! good times.
Was This Post Helpful? 0
  • +
  • -

#9 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 04:16 PM

Haha it's a good thing you're asking yourself the questions. I've had, and seen quite a few others have, pretty bad headaches trying to figure out a problem with variables being null for "some reason", only to find this is the root cause :P
Was This Post Helpful? 0
  • +
  • -

#10 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 04:31 PM

:P
Dude, i remember my inheritance class. I was NOT good AT ALL. Didnt understand a thing. And i'm learning it now, all by myself. And everything has started to fall in place.
I realized where it went wrong back then. My teacher spent two week on arrays. And i was already good with arrays as you dont get to Java until you have C++ done in my school. Only one week for inheritance and a final project on it. I was so beat up. Even though i got a B+ in the class. I didnt know jack S*#t about it.
Was This Post Helpful? 0
  • +
  • -

#11 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: What is "this" statement? Can somebody give me an easy example

Posted 21 July 2011 - 09:42 PM

How many times a week did you have class? Two weeks for arrays but only a week for inheritance seems a bit silly.

I was lucky, my school taught C++ and Java at the same time. Usually the classes were a week or two out of sync, so it helped reinforce what was taught.

Things like this make me consider starting up a self-paced online "school", providing high quality education for free, so students can help themselves when the school they're attending won't. I've been considering it more and more over the past few months.
Was This Post Helpful? 0
  • +
  • -

#12 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Re: What is "this" statement? Can somebody give me an easy example

Posted 23 July 2011 - 11:50 AM

Twice a week.

I like to have a class for what i'm learning, but only to some extent. Not meaning, that i'd like them to stop at a point but to let give students some a sense of realization of what they're doing. Sometimes, i feel that some teachers are just getting their job done through some examples and making us see the pattern without telling us what they are actually meant for.

But it's whatever, can't do anything about it. At the end of the day, no matter what, its just you, books, pen and paper at a corner. If you're not doing that, not even the best of teahcers can help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1