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(Strin
You can use reflection as follows to access that non static field.
System.out.println("Enter name team");
Scanner tn = new Scanner(System.in);
Team team=new Team();
Field field=Team.class.getField("name");
field.set(team,tn.next());
System.out.println((String) field.get(team));
Live demo for reflection.
Or you can try as follows.
Team team = new Team();
team.name = tn.nextLine();
Live demo