Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java

Options
  • 24-02-2004 7:35pm
    #1
    Closed Accounts Posts: 11


    I have two classes, Server and Handle. Handle is created by calling its constructor from within Server. I want Handle to be able to be able to access the states and call methods in Server.

    I think I have to pass a reference to Handle that refers to Server but i am not sure exactly how to do this.

    Please help

    Ed


Comments

  • Registered Users Posts: 839 ✭✭✭Dr Pepper


    Declare a server object in the Handle class called parent of type server...
    Server parent;
    
    // Handle constructor:
    public Handle(Server parent)
    {
    this.parent = parent;
    }
    

    Create a new handle in server as follows...
    Handle handle = new Handle(this);
    

    The instance of server is passed to the child (handle) and all of the variables/methods can be accessed as follows..
    // In the handle class
    parent.whateverInt = 1;
    parent.setWhateverInt(1);
    // etc...
    

    All of the variables / methods you want to access in server from handle must be public AFAIK


  • Registered Users Posts: 6,309 ✭✭✭OfflerCrocGod


    Yep everything has to be public.....at that stage you may as well make everything static and just access stuff that way.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Originally posted by Dr Pepper
    All of the variables / methods you want to access in server from handle must be public AFAIK

    Unless you write Handle as a public inner class, in which case you could declare the methods private or protected. Then you would have to refer to Handle as Server.Handle if you want to work with it in another method.
    Originally posted by OfflerCrocGod
    at that stage you may as well make everything static and just access stuff that way.

    Never, never make a method call static just for the sake of it... Firstly because of the overheads it causes with large classes, secondly, in this case, it just wouldn't be approporiate.

    If hes going to be referencing one Server per handle class, the correct manner of going about it would be access\mutate methods.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by doodle_sketch
    Never, never make a method call static just for the sake of it... Firstly because of the overheads it causes with large classes, secondly, in this case, it just wouldn't be approporiate.
    Do you know why static methods have more overhead in Java? This strikes me as curious because in C++ generally the opposite is true.
    Is it related to Java being OO from the ground up (and static methods aren't very OO) where as C++ was more a matter of taking C and making it OO (among other changes)?


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Don't quote me on this, but I think that every time a static method is called, the class gets re-cached instead of referencing a pre-cached object (which is what occurs in a normal access\mutate situation).


  • Advertisement
  • Registered Users Posts: 6,309 ✭✭✭OfflerCrocGod


    Originally posted by doodle_sketch
    Never, never make a method call static just for the sake of it... Firstly because of the overheads it causes with large classes, secondly, in this case, it just wouldn't be approporiate.

    If hes going to be referencing one Server per handle class, the correct manner of going about it would be access\mutate methods.
    I know that's what I'd do, but in the context of this Q^ if the quick and nasty way works......use it, anyway the overhead I'd say (if this is being run on a PC - not PDA or Phone) would be minimal to unnoticable.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    I guess it is based on what the PC is doing.


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    Originally posted by edcasey15
    I have two classes, Server and Handle. Handle is created by calling its constructor from within Server. I want Handle to be able to be able to access the states and call methods in Server.

    I think I have to pass a reference to Handle that refers to Server but i am not sure exactly how to do this.

    Please help

    Ed

    Another method, that may or may not be appropriate in your situation would be to create an Interface that the Server extends. Specify any methods that you wish Handle to be able to use (in the interface) and implement them in the Server. Create a variable of type this interface in the Handle then do the same as mentioned above. Create the Handle passing in the Server's this to it.

    Handy way of seperating out the interface required.

    Gav


Advertisement