"This" points to other object? Why?

E

Ejeep

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;
}
 
J

John B. Matthews

Ejeep said:
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>
 
A

Andreas Leitgeb

Ejeep said:
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".
 
E

Ejeep

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".

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

Andreas Leitgeb

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".
 
R

Roedy Green

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;
}

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.
--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top