8 Replies Latest reply: May 26, 2009 12:58 PM by 807588 RSS

    Which class is a static method called on?

    807588
      Hi!

      I have this situation:
      public abstract class Person
      {
        public static Person[] getAll()
        {
          // How do I know which class this method was called on?
        }
      }
      
      public class Student extends Person
      {
      
      }
      
      public class Teacher extends Person
      {
      
      }
      
      Student[] students = Person.getAll();
      Teacher[] teachers = Person.getAll();
      As you might be able to figure out from the code. How can I return the correct objects, depending on which class I called getAll on?
        • 1. Re: Which class is a static method called on?
          807588
          depending on which class I called getAll on?
          Put a method getAll in Students, and a method getAll in Teacher. Or try the way java.util.Collection solved this problem with the method toArray(T[]).

          Edited by: Peter_vd_Wal on 26-mei-2009 13:34
          • 2. Re: Which class is a static method called on?
            masijade
            Edit: Nevermind. Static.
            • 3. Re: Which class is a static method called on?
              807588
              rejeep wrote:
              How can I return the correct objects, depending on which class I called getAll on?
              You can't. Forget it. There's no such thing as a "static polymorphism".

              For the fun of it, here's a similar way:
              abstract class Person {
                  private static final Map<Class<? extends Person>, Collection<Person>>
                      map = Collections.synchronizedMap(new HashMap<Class<? extends Person>, Collection<Person>>())
                  ;
                  
                  protected Person(){
                      Collection<Person> c;
                      Class<? extends Person> clazz = getClass();
                      
                      if( map.containsKey(clazz) ){
                          c = map.get(clazz);
                      }
                      else{
                          c = new LinkedList<Person>();
                          map.put(clazz, c);
                      }
                      
                      c.add(this);
                  }
                  
                  public static Person[] getAll(Class<? extends Person> c) {
                      return map.containsKey(c) ? map.get(c).toArray(new Person[0]) : new Person[0];
                  }
                }
                 
                class Student extends Person {   }
                 
                class Teacher extends Person  {   }
              ... but I don't think it'll help you much. For your design is flawed.

              df.
              • 4. Re: Which class is a static method called on?
                807588
                The problem is that I want to do stuff dynamicly. And Java is just not good at that.

                da.futt:
                Your solution works in my case.

                Thanks for the answers!
                • 5. Re: Which class is a static method called on?
                  807588
                  rejeep wrote:
                  The problem is that I want to do stuff dynamicly. And Java is just not good at that.
                  Actually Java is excellent at that if you don't use static where static doesn't belong.

                  I can't think, off the top of my head, of any language where a method signature can be chosen on the basis of return type.

                  Edited by: malcolmmc on May 26, 2009 1:17 PM
                  • 6. Re: Which class is a static method called on?
                    807588
                    rejeep wrote:
                    The problem is that I want to do stuff dynamicly. And Java is just not good at that.
                    @see malcommc.
                    da.futt:
                    Your solution works in my case.
                    I forgot one layer of synchronisation. You're welcome anyway.

                    df.
                    • 7. Re: Which class is a static method called on?
                      807588
                      rejeep wrote:
                      The problem is that I want to do stuff dynamicly. And Java is just not good at that.
                      Then try FM -- no static at all.
                      • 8. Re: Which class is a static method called on?
                        807588
                        malcolmmc wrote:
                        I can't think, off the top of my head, of any language where a method signature can be chosen on the basis of return type.
                        I seem to remember that Haskell does something like that, but since it was only in a 'Concepts of programming languages' course a few years back the details ellude me at the moment.