O'Reilly Forums: This - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

This Please don't reply.

#1 User is offline   MuffinMachine 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 28-April 12

Posted 30 September 2012 - 08:25 PM

I can see those not many replies happen in this place so here is a stone I'm dropping into a deep dark well as many others already have.

this.

I understand the concept of sending it to an instance in order to reference to the sending instance. That makes perfect sense. But I constantly see "this" used inside of a class to reference a variable that is already inside the class.

If a class has a global variable called "variable1" then why does it need to be referenced as this.variable1 from inside the class? It's not so much that I can't see why it would be done but I can't see why it would be done one time and not the next. For instance in the bee solution on page 260 I see:

this.shiftsToWork = numberOfShifts;

and right under it I see

shiftsWorked = 0;

both of these variables are global to the class, bother are private, both are int. There is nothing different about either variable. ANd yet one is referenced by this and the other is not.

If it doesn't need to be done then why the heck is it being thrown in there? Just to be confusing? To make us think? Because it's really distracting and in fact I've spent the last hour of my life trying to find the answer to this instead of learning the part of the book I'm on.

The book does not provide a good answer to this (at least not so far), microsoft help just says "this is used in order to qualify a field". Why does it need to be qualified? We know it's there or it wouldn't work. If I decide NOT to use this it's still going to pull the information from the field it was called from.

this.doesn'tMakeSense.
0

#2 User is offline   MuffinMachine 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 28-April 12

Posted 01 October 2012 - 04:55 AM

And now my future self will respond to my past self.

When everything starts to feel stupid leave the book open to the page where it feels stupid and then GO TO SLEEP.

Anyone who is having issues with this, here is the thing that I'm now seeing. Let's say you have a method like:

public int newMethod(int numbers)

and you also have a field in that class called:

int numbers

you can put a statement in the newMethod() class that reads like:

this.numbers = numbers;

And the instances numbers field will then be set to the methods parameter. In the book it looks like they are trying to use mostly as clarification. When two variable names seem to have similar meanings like:

shiftsToWork

and

numberOfShifts

they say:

this.shiftsToWork = numberOfShifts to make it clear which is the field and which is the parameter.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users