Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default how to maintain a list of static class and the instanciate from them

    hello,

    I have a list of uninstanciated class types and I wanted to call a static function from these
    "Class<? extends PropControl>" (without using reflection) that would create a new corresponding object instance
    but it appears I cant do that in java

    so now I want to create a factory that takes a class type as aparameter to create the corresponding object instance

    but this code dont compile:

    Java Code:
    public PropControl Create(Class<? extends PropControl> cls)
        {
            if(cls==HouseControl.class) <---- ERROR
            {
               here I create a new instance of HouseControl (that inherits PropControl)
            }
        }
    I get this error :

    incomparable types: Class<CAP#1> and Class<HouseControl>
    where CAP#1 is a fresh type-variable:
    CAP#1 extends PropControl from capture of ? extends PropControl


    how do I achieve this ?

    thanks
    Last edited by rompelstilchen; 02-26-2015 at 05:34 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to maintain a list of static class and the instanciate from them

    For what purpose do you need to do this? Sometimes, there may be an alternate approach which satisfies your requirement. And why is reflection not permitted?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default Re: how to maintain a list of static class and the instanciate from them

    ................
    Last edited by rompelstilchen; 02-26-2015 at 08:34 PM.

  4. #4
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default Re: how to maintain a list of static class and the instanciate from them

    Quote Originally Posted by jim829 View Post
    For what purpose do you need to do this? Sometimes, there may be an alternate approach which satisfies your requirement. And why is reflection not permitted?

    Regards,
    Jim
    reflection is not OO, besides, I am writing a game, so what if I need to port it to another language... who knows

    so the problem was that HouseControl was not overriding the proper class, it compiles fine now

    but I am surprised java does not permit a more OO implementation than a factory or the invoke method

    Java Code:
        public AtomControl Create(Class<? extends AtomControl> cls)
        {
            if(cls==HouseControl.class)
            {
                return HouseControl.Instanciate(null, null, null);
            }
            return null;
        }
    I gonna have to check for all classes types with an "if" branch..
    Last edited by rompelstilchen; 02-26-2015 at 08:26 PM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to maintain a list of static class and the instanciate from them

    Quote Originally Posted by rompelstilchen View Post
    reflection is not OO, besides, I am writing a game, so what if I need to port it to another language... who knows
    So you can't use a hammer and saw to build a deck because you may be someplace where you don't have a hammer and saw and have to build another deck. Use the tools you have, when you have them. Worry about the other situation when the time arises. There is no guarantee that all the constructs you use in Java will be supported by other languages either.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default Re: how to maintain a list of static class and the instanciate from them

    finaly I used invoke althought I read on forums a million times that instanceof/invoke/you name it is not OO and not best practive in java

    something similar to invoke is using isAssignableFrom but I still have to write a new if clause everytime I create a new class

    it is super anti OO...
    Last edited by rompelstilchen; 02-27-2015 at 11:35 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: how to maintain a list of static class and the instanciate from them

    You haven't explained what problem this method is supposed to be solving.
    What is it that is using this method?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to maintain a list of static class and the instanciate from them

    Quote Originally Posted by rompelstilchen View Post
    but this code dont compile:

    Java Code:
    public PropControl Create(Class<? extends PropControl> cls)
        {
            if(cls==HouseControl.class) <---- ERROR
            {
               here I create a new instance of HouseControl (that inherits PropControl)
            }
        }
    I get this error :

    incomparable types: Class<CAP#1> and Class<HouseControl>
    where CAP#1 is a fresh type-variable:
    CAP#1 extends PropControl from capture of ? extends PropControl
    Huh? this compiles (and runs) fine.
    Java Code:
    public class TestCreateClass {
      
      public static void main(String[] args) {
        new TestCreateClass().create(Derived.class);
      }
    
      public Base create(Class<? extends Base> clazz) {
        if (clazz == Derived.class) {
          return new Derived();
        }
        return null;
      }
    }
    
    class Base {
    }
    
    class Derived extends Base {
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to maintain a list of static class and the instanciate from them

    Quote Originally Posted by rompelstilchen View Post
    so the problem was that HouseControl was not overriding the proper class, it compiles fine now
    Ah, ok then.

    Note that a class may extend another class; methods may be overridden.

    Also note that by convention, method names in Java start with a lowercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to maintain a list of static class and the instanciate from them

    Like Jim and Tolls, I don't see why this construct should even be needed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to maintain a list of static class and the instanciate from them

    I see the word "game", so I'm guessing that this is an attempt to make the creation of game entity instances super-generic. Its not unthinkable to want this, if for example you have game data such as a level in which you encode the entity names, its actually useful to be able to construct instances through reflection as you load in the level data.

    Why you would then not go ahead and use reflection, I don't know. Even with the self-imposed limitation of "need to be able to port this to other languages" - the solution chosen right now to just do a flipping huge if-else tree is already the cumbersome way that can be used as the alternative should you be faced with that very unlikely scenario.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: how to maintain a list of static class and the instanciate from them

    If there is going to be a long chain of if/else if to explicitly test for what type object, why not pass the name of the class as a String?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: how to maintain a list of static class and the instanciate from them

    You don't need the name of the class. If it extends AtomControl, you can just create a new instance with newInstance(). If it does not extend AtomControl, it won't even compile due to the generic parameter type.

    Example (minus the try/catch):
    Java Code:
    public AtomControl create(Class<? extends AtomControl> cls)
    {
       return cls.newInstance();
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 2
    Last Post: 11-23-2014, 03:59 PM
  2. How to maintain array list if its change dynamically.
    By ganesh.gothi@gmail.com in forum Java Applets
    Replies: 2
    Last Post: 05-24-2012, 05:11 PM
  3. Can non-static Inner Class have static fields ?
    By fatabass in forum New To Java
    Replies: 3
    Last Post: 02-05-2012, 09:47 PM
  4. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  5. Replies: 3
    Last Post: 02-09-2010, 05:22 AM

Posting Permissions

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