How to reference non-static variable name from static context?

前端 未结 5 647
闹比i
闹比i 2021-01-21 02:22

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         


        
5条回答
  •  悲&欢浪女
    2021-01-21 02:33

    name is a team name. So you need to instantiate a new Team object and set its name :

    public static void main(String[] args) {
        System.out.println("Enter name team");
        Scanner tn = new Scanner(System.in);
        Team team = new Team();
        team.name = tn.nextLine();     
    }
    

提交回复
热议问题