You are not setting value to the object’s name variable.
public static void main (String[] args) {
Playeri user = new Playeri();
Enemyu enem = new Enemyu();
Scanner input = new Scanner(System.in);
user.name = input.nextLine();
user.showName();
enem.showUserName();
}
}
class Playeri {
String name;
void showName() {
System.out.println("Your name is " + name + ".");
Enemyu.enemUser.name=name; // Set it like this
}
}
class Enemyu {
static Playeri enemUser = new Playeri(); // make it static
void showUserName() {
System.out.println("Hey, bro! Are you " + enemUser.name + "?");
}
}
Output –
John
Your name is John.
Hey, bro! Are you John?
This solves your problem, but It is recommended you use setter-getter method.
5
solved How to give a variable from another class a value from main method?