Global variables in Java

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

How do you define Global variables in Java ?

24条回答
  •  悲&欢浪女
    2020-11-22 12:25

    Creating an independent file, eg. Example.java to use the 1st solution, is just fine. You can do that also within the app, if e.g. the global variables are special to your current app, etc.:

    Create a class at the beginning and declare your variables in there:

    class Globals {
      static int month_number;
      static String month_name;
    }
    

    You can then access these variables -- using them as 'Globals.month_number', etc. -- from averywhere in your app.

提交回复
热议问题