简体   繁体   中英

Pass String from One class to another class

I have this 1st clas:

 private class proceedAL implements ActionListener
{
public void actionPerformed(ActionEvent z)
    {
        String x = (String)mouseB.getActionCommand();
        String y = (String)monitorB.getActionCommand();
        ComputerSimulator me = new connect(x,y);
    }

}

and another class:

public class ComputerSimulator extends JFrame
{
public void connect(String x, String y)
{
   String i, j;
   c2.setText(x);
   c3.setText(y);
}

This error appears: cannot find symbol - class connect

What am I missing? I'm just a starter in Programming, need some help guys

The correct way to instantiate an object of your class and call its method is this:

ComputerSimulator e = new ComputerSimulator();
e.connect(x, y);

connect indeed is not class. It is a method in class ComputerSimulator .

You should create object first:

ComputerSimulator me = new ComputerSimulator()

now you can call method connect : me.connect(x, y);

connect is not a class, its a method on the class ComputerSimulator . You are invoking the connect method as if it were the constructor, you need to do

ComputerSimulator computerSimulator = new ComputerSimulator();
computerSimulator.connect();

You are having it wrong, you can only make new of a class (like ComputerSimulator ).

Once you have an instance of that class, you can use its public methods (like connect ) -> me.connect(x, y);

By the way, throws refers only to exception handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM