Results 1 to 4 of 4
- 01-07-2011, 05:34 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
object sending itself as a parameter
in my public class Person, I have this chunk of code
public void murder(Person victim, Person murderer)
{
victim.die();
victim.culprit= murderer;
}
it causes the victim (an other person) to die, and also for the now dead victim to know who the murderer was. each Person has a field culprit of type person.
my issue is that in the main method it always looks like
p1.murder(p2,p1);
note p1 is sending itself as a parameter. Is there anyways to get around this?Last edited by appleLove; 01-07-2011 at 05:37 AM.
- 01-07-2011, 05:36 AM #2
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Using the magical 'this' keyword
so p1.murder(p2) will now work!Java Code:public void murder(Person victim) { victim.die(); victim.culprit = this; }
- 01-07-2011, 05:37 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Awesome. thankyou so much :)
- 01-07-2011, 04:48 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Sending object through socket
By Alexandrinne in forum New To JavaReplies: 0Last Post: 11-15-2010, 07:03 AM -
sending object through network/socket
By skandalouz in forum NetworkingReplies: 1Last Post: 12-24-2009, 07:34 AM -
Sending an object via sockets - all fields but array updating
By Wassa in forum NetworkingReplies: 6Last Post: 12-29-2008, 03:14 AM -
passing a parameter
By aarthi2learn in forum AWT / SwingReplies: 4Last Post: 12-22-2008, 05:46 AM -
Can a method take itself as parameter?
By bukake in forum New To JavaReplies: 10Last Post: 09-06-2008, 09:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks