Results 1 to 15 of 15
- 04-06-2011, 05:15 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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
- 04-06-2011, 05:22 PM #2
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
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
- 04-06-2011, 05:24 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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..
- 04-06-2011, 05:32 PM #4
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
can you send the coding of classes B and C please ?
- 04-06-2011, 06:44 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
Try adding this.
B variableName = new B();
- 04-06-2011, 07:05 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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...
- 04-06-2011, 10:02 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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
- 04-06-2011, 11:37 PM #8
It may be a smelly design but in case it isn't you can do this:
Now the B object has a reference to the C object and vice-versa.Java Code:B bb = new B(); C cc = new C(); bb.method(cc); cc.method(bb);
- 04-07-2011, 05:03 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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
- 04-07-2011, 05:16 AM #10
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.
- 04-07-2011, 05:34 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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...
- 04-07-2011, 05:47 AM #12
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.
- 04-07-2011, 05:51 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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..
- 04-07-2011, 05:58 AM #14
Try searching for the MVC (model view controller) pattern.
- 04-07-2011, 05:59 AM #15
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Help with accessing static method
By jazzermonty in forum New To JavaReplies: 9Last Post: 03-07-2011, 09:04 PM -
accessing a singleton class inside another class
By katturv in forum New To JavaReplies: 1Last Post: 12-08-2010, 06:23 PM -
Need help with accessing files in a method
By t0nydanzuh in forum New To JavaReplies: 6Last Post: 10-03-2010, 06:05 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Inner class accessing outer class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 08:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks