Call one class method from another class Java

Ok, very quick question here.. Suppose i want to call one class method from another class (2 classes are in the same package). is there any other way of doing this apart from creating an object of the class whose method i want to call??I have a client/server system..the client sends a string, the server echos it back, i then want to append this message to the clientGUI textarea. The message is sent to Client class however, if i want to call the method in the ClientGUI class, i would have to create a new object of that class, hence the old ClientGUI is gone. Any ideas?
Thanks
oggiemcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

If the method is not depndednt on the instance of the class - then make this method static and call it by name


But in your case - you want to show on some particular text araea of particular client - I'm guessing that you dont need to create new client - you need
to access the same objecvt you user is seeing on the screen to modify particular textarae and not the textarae on the newly created
client objkect
0
for_yanCommented:
As I understand your communication happnes not between classes - it hapopens between instances of classes.
So you are sneding request oform particular insatance of CilentGUI, then response should be recievd by the same
instance of ClientGUI and shown on the textarea belonging to that intsance.
So you should not create new instance of ClientGUI - that would not be right.
0
for_yanCommented:

>The message is sent to Client class

This cannot be correct. Any class is abstraction - the template how to build object.
You cannot send mesaage to the template. You can send message to the instance of the class, to real object built according to that template.
And you should send it to that very instance which also owns the textarea your user will see on the screen.
That's why it is not a matter of creating new instance. It is a matter of arranging
communication in such a way that uyour existing instance, which sent the message should
also receive the response.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

oggiemcAuthor Commented:
for_yan
>>>The message is sent to Client class
yes, i meant "instance of Client class"
I want the same Client class instance that sent the message to the server, to receive the echo so it can append it to that particular ClientGUI instance textarea. The problem i have is, Client class (which communicates with server), is seperate to ClientGUI class, whose textarea i want to access!! Any ideas? Hope i made that a bit clearer..
0
for_yanCommented:

Are you instatiating the Client class ferom ClientGUI class?

Or how do you communicate the contets of the message form ClientGUI to Client to send it ?
0
oggiemcAuthor Commented:
Yes, i instantiate from the ClientGUI class and then call the Client class from that object..
0
for_yanCommented:


The reason I'm asking the questions above is that when you communciate the message to the client
(eiether in the Client constrctor or in some method) - you can use the similar method - either in constructor
or in parallael method to communicate the reference to to your ClientGUI instance.
Then when you receive te response you use the same reference to accecs the ClientGUI instance and in partucular to modify the contents of text area.
0
for_yanCommented:
It can be like that:

public class ClientGUI {
JTextArea txt;

public ClientGUI () {



txt = new JTextArea(30,40);
...
Client  c = new ClientGUI(this);

...

c.sendMessage("tratatat");





}


public void updateTextArea(String s) {

txt.setText(s);

}

}


public class Client {

ClientGUI cg;


public Client(ClientGUI cg){

this.cg = cg;


}

// somewhere you got the response - in some nmethod ovf Clinet

//then you say

cg.updateTextArea(response);


}

]

Open in new window



Let me know if you don't understandt - I'll answer your questions
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Can you post your current code so that we may provide some more precise assistance?
0
oggiemcAuthor Commented:
Ah yes!!! So  when i instantiate the Client class, send a reference of the ClientGUI instance to the Client class constructor!! Thank you, now i understand what this refers to when placed as an argument to a method/constructor!! Two birds killed with the one stone!! Points coming ur way!! Thanks. I have another question about my ClientGUI freezing up when i try to transmit a second message, you might have a look when you have a chance :)
0
for_yanCommented:

Great! It is good you undestand - thsi is very crucial thing - similar things apply to many cases
in Java, not necessarily with sockets, etc, but just when you have two object iinteracting (almost in every appliaction).
Lots of people initially make such mistake and tend to create a new uindepdnednt instance of the objcet instead of passing the reference.
Annd that of course makes all the difference in the world.

Yes, I looked at your other question - it unfortunately seems much more difficult. I'll try to look again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.