Acees to object and method problem

Hello,

I have3 filesprog1.java , prog2.java and prog3.java in 3 different packages.

prog1.java creates an instance of prog2.java.

prog1.java also creates an instance of prog3.java

prog2.java is the gui which i need to get input from a user and pass it to a method in the instance created from prog3.java

I cannot access the instance or method of prog3.java from prog2.java

Helpe me...

[430 byte] By [dcreedona] at [2007-10-3 3:32:59]
# 1
I think you can use observer pattern. http://en.wikipedia.org/wiki/Observer_pattern http://www.javaworld.com/javaworld/javaqa/2001-05/04-qa-0525-observer.html
Bina_Nusantaraa at 2007-7-14 21:27:19 > top of Java-index,Java Essentials,New To Java...
# 2

ok,

prog2.java accepts a user name and password via dialog box, i want to call a method in prog3.java and pass it the user name and password from prog2.java.

I am listening for the login button action event which signals the username and password have been entered, I was hoping to call the method from the actionlistener in prog2.java, as the prog3.java object has already been created to allow the call to the method I was hoping it would be a bit simpler.

As I am new to java your suggestion may very well be 100% right, but it doesn't feel right to me to have prog3.java object waiting for an event like the button click from the GUI, hmmm...

dcreedona at 2007-7-14 21:27:19 > top of Java-index,Java Essentials,New To Java...
# 3

it doesn't matter whether they are in same package or different packages as long as we can resolve the scope properly . Here I assum e that Prog1.java is top level container which is holding the instacnes of prog2 and prog3 . now some action of prog2 should invoke some method on prog3 . Here prog2 doesn't know the existence of prog3 as two are independent and only thing thats linking both of them is Prog1.So its staright forward that any such communication should happen through Prog1. While instantiating Porg2 set the reference of Prog1 in prog2 . soemthing like Prog2 p2 = new Prog2(); p.setTop(this);

Ofcourse this involves code change in Porg2 . then from the listenere you can simply call method m3 in prog3 like p2.getTop().p3.m3(); hope this helps..

balaji31a at 2007-7-14 21:27:19 > top of Java-index,Java Essentials,New To Java...
# 4
hiya, this sound more like the solution I would have expected, let me try it out and I will get back to you.Thanks
dcreedona at 2007-7-14 21:27:19 > top of Java-index,Java Essentials,New To Java...
# 5

why not pass instance of prog3 to prog2 instead of prog1.

what about coupling?

> While instantiating Porg2 set

> the reference of Prog1 in prog2 . soemthing like

> Prog2 p2 = new Prog2(); p.setTop(this);

> Ofcourse this involves code change in Porg2 . then

> from the listenere you can simply call method m3 in

> prog3 like p2.getTop().p3.m3(); hope this helps..

mchan0a at 2007-7-14 21:27:19 > top of Java-index,Java Essentials,New To Java...
# 6

ok, i'm back, i had a quick look at interfaces and that does not seem to be the way to go. Can someone advise me the best way to call a method in one class from another class ? or point me in the direction of a sample application with more than one java file and a gui that sits on top accepting input which is then passed to various methods in the application?

dcreedona at 2007-7-14 21:27:19 > top of Java-index,Java Essentials,New To Java...