• Post Reply Bookmark Topic Watch Topic
  • New Topic

sometimes .this.method() and sometimes .method()  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Migrated from C++ to Java,
Working with Eclipse,

Why is it that to use some methods I use

someObject.method()

but sometimes I need to use

someObject.this.method()

?

Many thanks.
[ October 13, 2008: Message edited by: blingo james ]
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by blingo james:
Hello
Migrated from C++ to Java,
Working with Eclipse,

Why is it that to use some methods I use

someObject.method()


The method belongs to some other Object (instance of a class), and you need to call the method that works on/from that object.

Originally posted by blingo james:
but sometimes I need to use

someObject.this.method()


More appropriately that should be:
SomeType.this.method()

In these cases, code is executing in an instance of an inner class - a class that only lives as part of an instance of the outer class. The inner class has access to the outer class' methods, but you have to tell it that the method you want to call belongs to the outer class' object, and that is what SomeType.this does - basically 'call method() on my reference to the Object of SomeType that I belong to'.
 
blingo james
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.

To understand, I've tried to write something like this:



assuming I have



Can someone give an example of using 'this' ?

Also - when using 'this' I understand I should use classes and not instances,
but doesn't it mean that the classes should be static?

Many thanks.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do some searches through the Ranch for use of this. You find 1 2 3.

When you have to say Foo.this.bar(); it means you are inside an inner class, and you are trying to get at an instance member of the object from the outer class. I don't know whether it makes any difference if the inner class is an instance class or a nested class (static).
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by blingo james:
Also - when using 'this' I understand I should use classes and not instances,
but doesn't it mean that the classes should be static?

Many thanks.


No, it is more of just a name-space identifier. As a matter of fact, the class must NOT be static, because static nested classes don't have an instance of the outer class they belong to - they are static so there is no OuterClass instance, and so no OuterClass.this.
 
blingo james
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll look in the links on hopefully learn this, I get the feeling
it's not 100% the same THIS as C++...
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example using 'this', the way you asked:

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by blingo james:
. . . not 100% the same THIS as C++...
Java isn't C++. Assume nothing in Java is the same as it is in C/C++.
 
blingo james
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Java isn't C++. Assume nothing in Java is the same as it is in C/C++.


LOL I'll try to keep it in mind
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!