Java 如何从另一个类中的void方法调用变量

Java 如何从另一个类中的void方法调用变量,java,class,constructor,Java,Class,Constructor,如何从另一个类中的void方法调用字符串变量user 我经常在运行这段代码时出错,因为getUser()函数中的user不等于action(ActionEvent j)函数中的user 阿尔法类: public class Alpha(){ private string user; public Alpha(int temp){ temp = 0; } public void action(ActionEvent e){ String command = e.getActionComman

如何从另一个类中的void方法调用字符串变量user

我经常在运行这段代码时出错,因为getUser()函数中的user不等于action(ActionEvent j)函数中的user

阿尔法类:

public class Alpha(){

private string user;

public Alpha(int temp){
temp = 0;
}

public void action(ActionEvent e){
String command = e.getActionCommand();
user = "hi";
}

public String getUser(){
return this.user;
}
public class Alpha(){

private string user;

public Alpha(int temp){ why do you need a temp argument?
temp = 0; //why do you set temp at 0?
}

public void action(ActionEvent j){ // why do you need an ActionEvent j argument?
user = "hi";
}

public String getUser(){
return this.user;
}
主要内容:


以下是所有问题的总结:

阿尔法类:

public class Alpha(){

private string user;

public Alpha(int temp){
temp = 0;
}

public void action(ActionEvent e){
String command = e.getActionCommand();
user = "hi";
}

public String getUser(){
return this.user;
}
public class Alpha(){

private string user;

public Alpha(int temp){ why do you need a temp argument?
temp = 0; //why do you set temp at 0?
}

public void action(ActionEvent j){ // why do you need an ActionEvent j argument?
user = "hi";
}

public String getUser(){
return this.user;
}
主要内容:


用户保持空值,因为您从未为其分配对象。是否尝试在
操作
方法中设置
用户
的值?如果是,则将其更改为
this.user=“hi”。我从未为其分配对象是什么意思?您分配的是一个局部变量,而不是类字段。很抱歉,我刚刚编辑了它应该如何修改其ActionEvent e,它太包含一个名为getActionCommand()的方法。我没有包括我所有的代码,因为没有所有的代码它太长了很难理解。你至少应该提供一个工作实例。但据我所知,这可能会给你们带来问题:公共无效操作(ActionEvent e){this.user=“hi”}