How to reference non-static variable name from static context?
I am trying to write a code that lets the user enter a team name. Here is
my code:
public class Team {
public String name;
public static void main(String[] args) {
System.out.println("Enter name team");
Scanner tn = new Scanner(System.in);
name = tn.nextLine();
}
}
I understand that "non-static variable name cannot be referenced from a
static context". I know that if I take the "static" away from the main
then it will work, but:
a) How can I reference it without taking the "static" out?
b) Is there a way to get the users input and assign it straight to the
variable "name" i.e. without the:
Scanner tn = new Scanner(System.in);
name = tn.nextLine();
Basic questions I know, but I am still a beginner! Many thanks, Miles
No comments:
Post a Comment