Global variables in Java

2018-12-31 16:34发布

How do you define Global variables in Java ?

标签: java
19条回答
大哥的爱人
2楼-- · 2018-12-31 17:16

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.

查看更多
登录 后发表回答