Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Identifying calling object

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Identifying calling object

    Hey guys!

    I'm trying to do this:

    int x;
    x = orc.getFrags;  //orc extends player

    In class player I have:
    public int getFrags(){
     
        If (this.getClass().getName().equals("orc")){
            return 5;
        }
    }

    But it doesn't work. I'm guessing its because "this" returns the class player and not the subclass orc. Is there any way to determine which object/subclass called a method from a superclass?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Identifying calling object

    Add a println in the getFrags method to print the class name, you will then see which class it is referring to, and what the name actually is.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Identifying calling object

    It does indeed return the class "player". Is there any way to find out if the method was called with regards to a subclass like orc, elf etc?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Exclamation Re: Identifying calling object

    Quote Originally Posted by frogfury View Post
    It does indeed return the class "player". Is there any way to find out if the method was called with regards to a subclass like orc, elf etc?
    This is not the behavior on my machine. Is orc just a reference to an instance of Player, or is it actually a class which extends Player? What exactly are you trying to accomplish

    public class Test{
    	public static void main(String[] args) throws Exception{
    		Player pla = new Temp();
    		pla.getFrags();
    	}
     
    	private static class Player {
    		public void getFrags(){
    			System.out.println(this.getClass().getName());
    		}
    	}
     
    	private static class Temp extends Player{
     
    	}
    }

    Prints out Temp, not Player

  5. The Following User Says Thank You to copeg For This Useful Post:

    frogfury (October 25th, 2010)

  6. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Identifying calling object

    Apart from all that, if you need to know which subclass's method is invoked, it's almost always a sign of misuse of inheritance.

    db

  7. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    frogfury (October 25th, 2010)

  8. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Identifying calling object

    Reading copeg's example I just realized that I had set my main class to private. No wonder it wasn't working...

    Thanks a lot for your time guys!

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Identifying calling object

    Quote Originally Posted by Darryl.Burke View Post
    Apart from all that, if you need to know which subclass's method is invoked, it's almost always a sign of misuse of inheritance.

    db
    ^^ this is very important to keep in mind, as this is the basis for creating well-formed polymorphic classes, as well as properly using inheritance.

    If you want a way to differentiate the name of different players, put a string field in your base class.

    public abstract class Player
    {
        public final String name;
     
        public Player(String name)
        {
            this.name = name;
        }
    }

    public class Orc extends Player
    {
        public Orc()
        {
            super("orc");
        }
    }

    public static void main(String[] args)
    {
        Player p1 = new Orc();
        Player p2 = new Wizzard(); // code for Wizzard not provided, but it's something similar to Orc
        System.out.println(p1.name);
        System.out.println(p2.name);
    }

    If you have a set list of names possible, simply use an enumeration instead of a string.

Similar Threads

  1. calling SingleFrameApplication
    By petem86 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2010, 09:06 AM
  2. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  3. calling a constructor
    By turnwellm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2010, 08:46 PM
  4. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM