0

This question is not a duplicate

This question is not related to what "this" means in Java. It is a question about differing syntax for properties that I'm seeking to clarify based on my understanding of another language. I have been working through the android docs and saw the same property referenced differently and I wondered why.

Question

In Objective-C when a property is declared, it autosynthesizes getters and setters that are then accessed via a dot syntax like so:

self.someProperty;

However, in the background, this is really calling:

[self someProperty];

- (id) someProperty {
    return _someProperty;
}

// or

[self setSomeProperty:someValue];

- (void) setSomeProperty:(id)someProperty {
    _someProperty = someProperty;
}

Part of the autosynthesization also generates a variable with a '_' prefix which you can access directly. So to summarize,

_someProperty;
// and
self.someProperty;

Often refer to the same variable; however, in truth, self.someProperty calls the method and _someProperty accesses the memory directly. In Java, if I declare a property at the top of my file like this:

private String someStringProperty;

Is there some sort of autosynthesization in the background that would make these to statements different?

someStringProperty = new String();
// or
this.someStringProperty = new String();
Logan
  • 52,262
  • 20
  • 99
  • 128
  • There's not much automatic about Java at all, except maybe auto boxing. If you want an accessor method, you write one, and call it using normal method syntax. This us ask covered in any Java tutorial, though. – Dave Newton May 13 '14 at 01:17
  • Thanks @DaveNewton - I must have missed that part, I'm still in the process of learning. I just saw it used twice differently in the same class in the Android docs and I was curious if there might have been a reason. – Logan May 13 '14 at 01:24
  • @Vash - My question isn't really about the meaning of "this" and those answers didn't answer my question about whether or not the calls were different in the background. – Logan May 13 '14 at 01:54
  • I don't understand why this was downvoted and closed. The question referenced as a duplicate is not a duplicate. If you read my question, as well as that question and the answers, you will see that I'm requesting clarification on a specific concept that is related to the language. There is no confusion on my part, or within the question, about what "this" means. I believe people glanced at the question and downvoted / closed because they made an assumption. – Logan May 13 '14 at 01:58
  • I'm going to reopen, but I understand why it was closed--it really is a duplicate in essence, but the approach is significantly different. – chrylis -cautiouslyoptimistic- May 13 '14 at 02:10

2 Answers2

1

There is no difference. When you use "this" you are using this object. Using "this" is not making method call.

someStringProperty = new String();
// or
this.someStringProperty = new String();

Both these statements are equal. use of "this" just makes it more readable.

vkg
  • 1,839
  • 14
  • 15
0

In Java, this refers to the "current" object, that is, the object to which the method or constructor being called belongs. Usually you would use this.someProperty instead of just someProperty is to distinguish between local variables and properties of the object (see http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html for an example).