Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java- class loading help

    please remember that i am just a teenager in a high-school.

    My friend and I are trying to make this text-based tactics game with java. Everything is going pretty smoothly, except one.

    We are trying to make an user interface, via console, as interactive as possible. So, we need quiet of commands. How would you manage all these commands? I was thinking about shoving all the classes into Object[] and calling them as users need it, but we hit the wall when I figured out that it is not possible to instance them all automatically.

    What is a good way to manage insane amount of class files and calling them as they fit? Or, should i just paste the whole program of 20000 if/else statements?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I think that it might help a lot if we could see some code. If you're thinking that you'll need 20,000 if/else statements then it seems to me that you might want to rethink your approach.

  • #3
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, it's entirely possible to get class names using RTTI (as usual, I'll refer you to Chapter 10 of Thinking in Java, 3rd Edition which is free in electronic format), but as AntonioHawk said you'd probably be better off attempting this in a different way. Interfaces and Inheritance exist for a reason (or many reasons) after all, but without any code or even a structural explanation of any kind I can't say for sure.

    Simple RTTI methods include the instanceof keyword, Class.isInstance() [which is functionally identical to instanceof], Class.forName(), <your class>.getClass() and <your class>.class.
    Class.forName(), .class, and .getClass() will return Class objects.
    Reflection could be more than you need but could prove useful depending on how you're building your application.

    You can check if a class is of a specific type using the instanceof keyword mentioned above:
    if (someObject instanceof MyClass) {
    // do something
    }

    Note that instanceof and isInstance() will return true if someObject is of a class that is derivative of MyClass while comparison of Class objects will not.

  • #4
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that helped. I thank you both. hooah!


  •  

    Posting Permissions

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