Results 1 to 6 of 6
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default Compiling with Different Classes

    I'm not exactly sure how to explain what I am trying to do so I'll just say it and see if it makes any sense.

    I have a Main.java that creates two different classes. A Player1 and Player2 class. My problem is that these 2 classes code can change each time I compile. For example, the project I'm working on is a very small game that incorporates two players using their own AIs.

    So they'll be uploading their .java AI file to maybe say a webserver somewhere and I can download it and use it in the program.

    How is it possible that I hook in their code to that Player1 or Player2 class?

    Any tutorial suggestions I can look at?

    The only thing I can come up with is that when they upload their code and I grab it off the server is to rename it to Player1.java before compiling. Any other suggestions?

    Thanks,

    Monc

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Interfaces.

    If you are familiar with interfaces (or perhaps even subclasses), you can have your Main.java interact with the interface for each player without worrying about what's going on behind the scenes. Of course, the classes for either player must implement the interface properly.

    Read more about it here: What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    (sorry, not very good at finding useful resources)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Just one clarification. What mean by the following line. How Main.java class can create two different classes?


    Quote Originally Posted by Moncleared View Post
    I have a Main.java that creates two different classes. A Player1 and Player2 class.

  4. #4
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    I just call an instance of each one:

    Player1 newPlayer1 = new Player1();
    Player2 newPlayer2 = new Player2();

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    That's not creating classes lol. That's call instantiate of classes. Simply means creating objects of classes. ;)

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well, to load in a class after the fact (after the program has started), you need to explicitly load it via a ClassLoader.

    Here's the exmaple the java api shows:
    Java Code:
    class NetworkClassLoader extends ClassLoader {
             String host;
             int port;
    
             public Class findClass(String name) {
                 byte[] b = loadClassData(name);
                 return defineClass(name, b, 0, b.length);
             }
    
             private byte[] loadClassData(String name) {
                 // load the class data from the connection
                  . . .
             }
         }
    Once you've loaded the class in this way, you use reflection to instantiate an instance.

    In the example above, you would call the 'findClass' method to get a Class object, and then call the 'newInstance' method on that class to create the instance of 'Player1' or 'Player2'.

    You will want Player1, Player2, etc. to implement a common interface, so you can just cast the result of newInstance to that type and interract with it.

Similar Threads

  1. Compiling and using jar file for custom classes
    By MAILMIRZA in forum New To Java
    Replies: 3
    Last Post: 01-12-2009, 04:56 PM
  2. note while compiling
    By j2vdk in forum New To Java
    Replies: 3
    Last Post: 09-03-2008, 11:52 PM
  3. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 09:32 AM
  4. Error during compiling
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:42 AM
  5. problems when compiling
    By valery in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:35 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
  •