Java中的全局variables

如何在Java中定义全局variables?

要定义全局variables,您可以使用静态关键字

public class Example { public static int a; public static int b; } 

现在您可以通过拨打电话从任何地方访问a和b

 Example.a; Example.b; 

你没有。 这是devise。 即使可以,你也不应该这样做。

这就是说你可以在名为Globals的类中创build一组公共静态成员。

 public class Globals { public static int globalInt = 0; /// } 

但你真的不应该:)。 真的..不要这样做。

另一种方法是创build一个这样的界面:

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

任何需要使用它们的类只需要实现这个接口:

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

你最好使用dependency injection:

 public class Globals { public int a; public int b; } public class UsesGlobals { private final Globals globals; public UsesGlobals(Globals globals) { this.globals = globals; } } 

真正的说,在Java OO程序中没有“GLOBAL”的概念

不过在你的问题背后有一些事实,因为在某些情况下你想在程序的任何部分运行一个方法。 例如— Phrase-O-Matic应用程序中的random()方法;它应该可以从程序的任何地方调用。

所以为了满足上面这样的事情“我们需要有全局的variables和方法”

声明variables为全局。

  1.Mark the variable as public static final While declaring. 

声明一个全球化的方法

  1. Mark the method as public static While declaring. 

因为我声明全局variables和方法是静态的,所以你可以通过下面的代码简单地调用它们

ClassName.X

:根据需求,X可以是方法名称或variables名称,ClassName是您声明它们的类的名称。

除了常量,什么都不应该是全局的。

 public class MyMainClass { public final static boolean DEBUGMODE=true; } 

把这个放在你的主类中。 在其他.java文件中,通过以下方式使用它:

 if(MyMainClass.DEBUGMODE) System.out.println("Some debugging info"); 

确保将代码从裁剪房间移开,并将其移出,或将注释掉此function。

如果你有一个主力方法,像一个随机数发生器,我build议创build一个“工具箱”包! 所有的编码器都应该有一个,然后只要你想在.java中使用它,只需要导入它!

很多很好的答案,但我想给这个例子,因为它被认为是更正确的方式来访问一个类的variables由另一个类使用getters和setters。 你这样用getter和setter来代替publicvariables的原因如下。 比方说,你的var将成为一个全局参数,你永远不会希望别人在你的程序执行过程中改变(当你正在开发一个团队的代码的时候),比如网站的URL。 从理论上讲,这可能会改变,可能会在你的程序中使用很多次,所以你想使用一个全局variables来一次更新它。 但是你不想让其他人进去改变这个var(可能没有意识到它有多重要)。 在这种情况下,您不需要包含setter方法,只包含getter方法。

 public class Global{ private static int var = 5; public static int getVar(){ return this.var; } //If you do not want to change the var ever then do not include this public static void setVar(int var){ this.var = var; } } 

一,Java中没有全局variables

二,我们所做的是一个“静态”关键字,这就是我们所需要的。 在Java之外,什么都不存在。 static关键字表示一个类variables,与实例variables相反,它只有一个副本,并且超越了所创build类的所有实例,这意味着它的值可以在所有实例中随时更改和访问。 如果你需要一个全局variables,它可以在范围之外访问,那么这就是你需要的variables,但是它的范围仅仅存在于类的地方,那将是全部。

Java中没有真正的全局variables。 每个静态variables都必须属于某个类(比如System.out),但是当你决定将要进入哪个类的时候,你可以在同一个类加载器的任何地方引用它。

请注意,更新时应始终保护静态variables以避免竞争状况。

 public class GlobalClass { public static int x = 37; public static String s = "aaa"; } 

这样你可以用GlobalClass.xGlobalClass.s访问它们

 public class GlobalImpl { public static int global = 5; } 

你可以打电话到任何你想要的:

 GlobalImpl.global // 5 

正如你可能从答案中猜测的那样,在Java中没有全局variables,你唯一能做的就是用静态成员创build一个类:

 public class Global { public static int a; } 

你可以在Global.a其他地方使用它。 但是,如果您使用Java 1.5或更高版本,则可以使用import static魔术来使其看起来更像真正的全局variables:

 import static test.Global.*; public class UseGlobal { public void foo() { int i = a; } } 

现在,这还不是一个最佳实践,所以你可以在广告中看到: 不要在家里做这个

Java中没有全局variables,但是有全局类和公有字段。 您可以使用java 5的静态导入function,使其看起来几乎像全局variables。

 // Get the access of global while retaining priveleges. // You can access variables in one class from another, with provisions. // The primitive must be protected or no modifier (seen in example). // the first class public class farm{ int eggs; // an integer to be set by constructor fox afox; // declaration of a fox object // the constructor inits farm(){ eggs = 4; afox = new fox(); // an instance of a fox object // show count of eggs before the fox arrives System.out.println("Count of eggs before: " + eggs); // call class fox, afox method, pass myFarm as a reference afox.stealEgg(this); // show the farm class, myFarm, primitive value System.out.println("Count of eggs after : " + eggs); } // end constructor public static void main(String[] args){ // instance of a farm class object farm myFarm = new farm(); }; // end main } // end class // the second class public class fox{ // theFarm is the myFarm object instance // any public, protected, or "no modifier" variable is accessible void stealEgg(farm theFarm){ --theFarm.eggs; } } // end class 

全局variables(也称为实例variables)是概念,它是类级variables,即它们是在一个类中定义的,但在外部方法中定义的。 为了使它们完全可用并直接使用它们提供static关键字。 所以,如果我正在写一个简单的算术运算程序,它需要一个数字对,那么两个实例variables定义如下:

 public class Add { static int a; static int b; static int c; public static void main(String arg[]) { c=sum(); System.out.println("Sum is: "+c); } static int sum() { a=20; b=30; return a+b; } } Output: Sum is: 50 

而且,在实例variables之前使用static关键字可以使我们不再为同一个variables指定数据types。 只需直接写入variables。

一般来说,全局variables(我假设你将它与C,Cpp进行比较)定义为public static final

喜欢

 class GlobalConstant{ public static final String CODE = "cd"; } 

ENUM在这种情况下也很有用:

例如Calendar.JANUARY

一般来说,Java编程没有任何全局variables。因为除了局部variables以外,所有的都是在程序中定义的任何类的范围之内的。 我们可以让静态variables具有全局variables的范围。

创build一个独立的文件,例如。 Example.java使用第一个解决scheme,就好了。 你也可以在应用程序中这样做,例如全局variables对你当前的应用程序来说是特殊的:

在开始处创build一个类,然后在那里声明你的variables:

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

然后,您可以从应用程序中的任何地方访问这些variables – 将其用作“Globals.month_number”等。

要定义全局variables,您可以使用静态关键字

 public final class Tools { public static int a; public static int b; } 

现在您可以通过拨打电话从任何地方访问a和b

 Tools.a; Tools.b; 

Yoy是正确的…特别是在J2ME中…您可以通过在MidLet构造函数(proggy initialization)中放入下面这行代码来避免NullPointerException:

 new Tools(); 

这确保了在使用它的任何指令之前分配工具。

而已!