Results 1 to 15 of 15
  1. #1
    denno is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default Accessing a method in another class

    The basic set up that I have is one class (e.g. A) that creates instances of two other classes (e.g. B and C), and I want to be able to access methods in B from C.. The reason I instantiate B and C in A is because A is my JFrame, and the classes B and C are in different BorderLayout positions.

    When class B is instantiated, it is passed a reference to class C, so I can access methods in C from B, but not the other way.

    Does anyone know how I could do this (access methods in class B from class C)?
    I've tried to keep the question as simple as possible, but if you want me to post my code, I can do that..

    Thanks
    Denno

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    if your methods in both classes B and C are public and not private then you can access and use them from the other class, i think that this is what you meen

  3. #3
    denno is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by baf06 View Post
    if your methods in both classes B and C are public and not private then you can access and use them from the other class, i think that this is what you meen
    No no, I don't want to access the methods written in classes B and C from class A, I want to access the methods written in class B from class C.

    As I created class B with a reference to class C passed to it, I can get to class C methods from class B, but I can't get to class B methods from class C..

  4. #4
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    can you send the coding of classes B and C please ?

  5. #5
    Professor is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    Try adding this.
    B variableName = new B();

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by denno View Post
    As I created class B with a reference to class C passed to it, I can get to class C methods from class B, but I can't get to class B methods from class C..
    Pass a reference to the class B instance into the class C instance.

    Alternatively, if they are both stored in A, and both have access to A, they should be able to access each other via A.

    The fact that you need to call C methods from B and B methods from C suggests a faulty design...

  7. #7
    denno is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Faulty Design? I would not at all be surprised lol.

    The program I'm after is an elevator simulation. Class A is the JFrame which contains the ButtonPanel class (B), the Elevator itself class (C) as well as other JLabels and things. I need for the Elevator class to let the ButtonPanel class know when a floor has been serviced, so that the colour of the button is changed back to it's default state.
    So a button is pressed, it turns yellow to indicate the press, the elevator moves to that floor and waits for some time (to pick up the passengers), and then the elevator moves on. I don't know how to change the button colour back to it's default state.

    Again, if this doesn't provide further insight, then I can upload my code, but I want to try and save everyone from having to read through it all..

    Thanks
    Denno

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    It may be a smelly design but in case it isn't you can do this:
    Java Code:
    B bb = new B();
    C cc = new C();
    bb.method(cc);
    cc.method(bb);
    Now the B object has a reference to the C object and vice-versa.

  9. #9
    denno is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    So I do this within class A yes?
    Is method() just any method within each of the classes B and C?

    Is there any better way I can design the program then to make this easier?

    Thanks for the help so far
    Denno

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    The names I used are just generic examples. You use whatever you like. Yes there is a better way to design it but since we have no idea what your program is or is supposed to do there is not much we can do. However, I doubt you will get much help in the way of design. We are not getting paid to do it for you.

  11. #11
    denno is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I know that you are using generic names, and as for the method, that's exactly what I was asking, if it was a generic name too.
    I was after hints or tips in regard to the design, so I appreciate your snide comment about not being paid...

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    What tips are you after. When doing design analysis there is no "one suit fits all" design. It has to be custom made. And as I mentioned we have no details about what you are doing so I have no idea what you expect from us.

  13. #13
    denno is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    As mentioned in a previous post, the design could be faulty, and I assumed this is because of where I'm instantiating the classes. I'll just keep searching the Internet for examples and see what I can come up with. The assignment has already been handed in anyway, but I really wanted to know how to do this part, just for personal satisfaction I guess..

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Try searching for the MVC (model view controller) pattern.

  15. #15
    denno is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

Similar Threads

  1. Help with accessing static method
    By jazzermonty in forum New To Java
    Replies: 9
    Last Post: 03-07-2011, 10:04 PM
  2. accessing a singleton class inside another class
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-08-2010, 07:23 PM
  3. Need help with accessing files in a method
    By t0nydanzuh in forum New To Java
    Replies: 6
    Last Post: 10-03-2010, 07:05 PM
  4. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  5. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 09:59 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
  •