Global variables in Java

后端 未结 24 2086
青春惊慌失措
青春惊慌失措 2020-11-22 11:56

How do you define Global variables in Java ?

24条回答
  •  迷失自我
    2020-11-22 12:37

    Another way is to create an interface like this:

    public interface GlobalConstants
    {
      String name = "Chilly Billy";
      String address = "10 Chicken head Lane";
    }
    

    Any class that needs to use them only has to implement the interface:

    public class GlobalImpl implements GlobalConstants
    {
      public GlobalImpl()
      {
         System.out.println(name);
      }
    }
    

提交回复
热议问题