This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes question on the use of 'this' ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question on the use of Watch "question on the use of New topic
Author

question on the use of 'this' ...

vijayender madura
Greenhorn

Joined: Jun 18, 2009
Posts: 13
Hi Guys,
i came across a situation like this in a project that i am working on...

abstract public class B {
public void method() {
...
...some implementation
}

public A extends B {
public methodInA() {
A.this.method();
}
}

Why do we use A.this here? how is it different from simply calling method() ... Since method() is not defined in A, it would obviously call the method that was implemented in the abstract super class 'B' ... Has 'A.this' got some special meaning???

Thanks in advance...


'Scientia est Potentia'
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
vijayender madura wrote:Hi Guys,

Why do we use A.this here? how is it different from simply calling method() ... Since method() is not defined in A, it would obviously call the method that was implemented in the abstract super class 'B' ... Has 'A.this' got some special meaning???

Thanks in advance...



Since A is extending B so all the methods of B would be available to A (Basic Inheritance property).this would always return the reference of current object .This program can also be run in this way also .


I think that it would be more clear to you !!

God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
OCPJP6
vijayender madura
Greenhorn

Joined: Jun 18, 2009
Posts: 13
actually ... u've kinda confused me...
but i found a solution to my doubt in this link ...

http://www.coderanch.com/t/382426/Java-General-intermediate/java/synchronize-classname-this

thanks anyway...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4161
    
  21

You might want to also check the context where you see that code. Here is an example of where using A.this.method() makes more sense:


There is an inner class (Inner) which has a method which calls a method in the Outer class. In this scenario there are two different 'this's to worry about. The Inner 'this' which has the something method, and the Outer 'this' which holds the Inner 'this', and inherits method() from the Parent. You would use the Outer.this qualifier to disambiguate (make clear) where the method is coming from.

You could use simply call:

But, since you have an Inner instance and an Outer instance, it can be hard to identify where that method is located (the compiler may know, but programmers won't). Especially in larger classes. So we will use:

To identify clearly that the method being called comes from the Outer instance, not the Inner instance.


Steve
 
 
subject: question on the use of 'this' ...
 
Similar Threads
Help!!!
How do you think this?
the difference of a method as void and not void concern a abstrct method.
inner class extends enclosing class
Abstract class