Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2009

Tip: Looking for answers? Try searching our database.

"This" points to other object? Why?

Thread view: 
Ejeep - 02 Jul 2009 10:42 GMT
I have a strange issue.When I use "This" to output the current object,
the console outputs the other object.
Who can help me analyse it?Why could it happen?
Please see the example below.
   The normal output is:  fh.a --this:fh@9d6
   but sometimes the output is: fh.a --this:an@c10

public class fh
{
   public fh(Image image)
   {
        a = image;
   }

   public void a(Graphics g1, int j, int k)
   {
       System.out.println("fh.a --this:"+this);
       g1.drawImage(a,......);

   }
   private Image a;
}
John B. Matthews - 02 Jul 2009 12:34 GMT
In article
<19750424-28e8-4b07-a8b7-02d4cd0e4a54@c9g2000yqm.googlegroups.com>,

> I have a strange issue.When I use "This" to output the current
> object, the console outputs the other object. Who can help me analyse
> it? Why could it happen?
> Please see the example below.
>     The normal output is:  fh.a --this:fh@9d6
>     but sometimes the output is: fh.a --this:an@c10

I am unable to compile your example. How did you get the output shown?
Can you supply an SSCCE <http://pscode.org/sscce.html>? Here is mine:

<code>
import java.awt.Image;

public class fh {

   private static Image a;

   public fh() {
       System.out.println("this: " + this);
       System.out.println("a: " + a);
   }

   public static void main(String[] args) {
       new fh();
   }
}
</code>
this: fh@66848c
a: null
<console>

Signature

John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Andreas Leitgeb - 02 Jul 2009 12:54 GMT
> I have a strange issue.When I use "This" to output the current object,
> the console outputs the other object.
> Who can help me analyse it?Why could it happen?
> Please see the example below.
>     The normal output is:  fh.a --this:fh@9d6
>     but sometimes the output is: fh.a --this:an@c10

You probably have two instances of that class.

One of these instances is an instance of "fh" itself, while
the other is really an instance of "an", which is probably
some subclass of "fh".
Ejeep - 02 Jul 2009 13:10 GMT
On Jul 2, 7:54 pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
wrote:
> > I have a strange issue.When I use "This" to output the current object,
> > the console outputs the other object.
[quoted text clipped - 8 lines]
> the other is really an instance of "an", which is probably
> some subclass of "fh".

Thanks for your reply.
These two classes are not relation of inheritance.
Andreas Leitgeb - 02 Jul 2009 13:26 GMT
>> >     The normal output is:  fh.a --this:fh@9d6
>> >     but sometimes the output is: fh.a --this:an@c10
[quoted text clipped - 3 lines]
>> the other is really an instance of "an", which is probably
>> some subclass of "fh".

> Thanks for your reply.
> These two classes are not relation of inheritance.

In that case, that other class "an"'s method has similar
code for logging in it, even wrongly claiming it was "fh.a".
Patricia Shanahan - 02 Jul 2009 16:41 GMT
>>>>     The normal output is:  fh.a --this:fh@9d6
>>>>     but sometimes the output is: fh.a --this:an@c10
[quoted text clipped - 9 lines]
> In that case, that other class "an"'s method has similar
> code for logging in it, even wrongly claiming it was "fh.a".

It should be easy to distinguish the two possibilities. Change the
wording in the fh.a println call:

System.out.println("Really, truly fh.a --this:"+this);

If all outputs show the new wording, then there must be at least one
class an that extends fh, and the "an@c10" output is coming from an
instance of such a class. If the "an@c10" output does not change to the
new wording, then it is coming from another call somewhere else in the
program.

Patricia
Roedy Green - 03 Jul 2009 01:46 GMT
>public class fh
>{
[quoted text clipped - 11 lines]
>    private Image a;
>}

You problem is in the code you did not show.  See
http://mindprod.com/jgloss/sscce.html

You did two things in the part you revealed to help confuse yourself
and your audience:

1. using a lower case class name.

2. use the symbol a to be both a field and a method.  There are plenty
of names to go around.
Signature

Roedy Green Canadian Mind Products
http://mindprod.com

"Deer hunting would be fine sport, if only the deer had guns."
~ William S. Gilbert of Gilbert and Sullivan



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.