Results 1 to 4 of 4
  1. #1
    appleLove is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

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

  2. #2
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Using the magical 'this' keyword
    Java Code:
    public void murder(Person victim)
    {
    victim.die();
    victim.culprit = this;
    }
    so p1.murder(p2) will now work!

  3. #3
    appleLove is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Awesome. thankyou so much :)

  4. #4
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default

    Thanks for the question. I too was having this problem but didn't think to ask :)

Similar Threads

  1. Sending object through socket
    By Alexandrinne in forum New To Java
    Replies: 0
    Last Post: 11-15-2010, 07:03 AM
  2. sending object through network/socket
    By skandalouz in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 07:34 AM
  3. Replies: 6
    Last Post: 12-29-2008, 03:14 AM
  4. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 05:46 AM
  5. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •