静态关键字可以在Java中以几种不同的方式使用,而且在几乎所有情况下它都是一个修饰符,这意味着它正在修改的东西可以使用,而不需要封装对象实例。.
Java是一种面向对象的语言,在默认情况下,编写的大多数代码都需要使用对象的实例。public class SomeObject {
public int someField;
public void someMethod() { };
public Class SomeInnerClass { };}
为了使用某些字段、方法或SomeInnerClass我必须首先创建一个SomeObject实例.public class SomeOtherObject {
public void doSomeStuff() {
SomeObject anInstance = new SomeObject();
anInstance.someField = 7;
anInstance.someMethod();
//Non-static inner classes are usually not created outside of the
//class instance so you don't normally see this syntax
SomeInnerClass blah = anInstance.new SomeInnerClass();
}}
如果我声明这些东西是静态的,那么它们不需要封装实例.public class SomeObjectWithStaticStuff {
public static int someField;
public static void someMethod() { };
public static Class SomeInnerClass { };}public class SomeOtherObject {
public void doSomeStuff() {
SomeObjectWithStaticStuff.someField = 7;
SomeObjectWithStaticStuff.someMethod();
SomeObjectWithStaticStuff.SomeInnerClass blah = new SomeObjectWithStaticStuff.SomeInnerClass();
//Or you can also do this if your imports are correct
SomeInnerClass blah2 = new SomeInnerClass();
}}
声明静态的东西有几个含义。
第一,在整个应用程序中只能有一个静态字段的值。.public class SomeOtherObject {
public void doSomeStuff() {
//Two objects, two different values
SomeObject instanceOne = new SomeObject();
SomeObject instanceTwo = new SomeObject();
instanceOne.someField = 7;
instanceTwo.someField = 10;
//Static object, only ever one value
SomeObjectWithStaticStuff.someField = 7;
SomeObjectWithStaticStuff.someField = 10; //Redefines the above set
}}
第二个问题是静态方法和内部类不能访问包围对象中的字段。(因为根本没有)。public class SomeObjectWithStaticStuff {
private int nonStaticField;
private void nonStaticMethod() { };
public static void someStaticMethod() {
nonStaticField = 7; //Not allowed
this.nonStaticField = 7; //Not allowed, can never use *this* in static
nonStaticMethod(); //Not allowed
super.someSuperMethod(); //Not allowed, can never use *super* in static
}
public static class SomeStaticInnerClass {
public void doStuff() {
someStaticField = 7; //Not allowed
nonStaticMethod(); //Not allowed
someStaticMethod(); //This is ok
}
}}
静态关键字也可以应用于内部接口、注释和枚举。public class SomeObject {
public static interface SomeInterface { };
public static @interface SomeAnnotation { };
public static enum SomeEnum { };}
在所有这些情况下,关键字都是多余的,没有效果。默认情况下,接口、注释和枚举是静态的,因为它们与内部类从来没有关系。
这只是描述了它们的关键字所做的事情。它没有描述关键字的使用是否是一个坏主意。这可以在其他问题中更详细地讨论,例如使用大量的静态方法是一件坏事吗?
关键字静态也有一些不常见的用法。有些静态导入允许您使用静态类型(包括接口、注释和枚举,而不是冗余标记的静态类型)。//SomeStaticThing.javapublic class SomeStaticThing {
public static int StaticCounterOne = 0;}//SomeOtherStaticThing.javapublic class SomeOtherStaticThing {
public static int StaticCounterTwo = 0;}//SomeOtherClass.javaimport static some.package.SomeStaticThing.*;
import some.package.SomeOtherStaticThing.*;public class SomeOtherClass {
public void doStuff() {
StaticCounterOne++; //Ok
StaticCounterTwo++; //Not ok
SomeOtherStaticThing.StaticCounterTwo++; //Ok
}}
最后,还有静态初始化程序,它们是类首次加载时运行的代码块(这是通常就在类首次在应用程序中实例化之前)和(像静态方法一样)不能访问非静态字段或方法。public class SomeObject {
private static int x;
static {
x = 7;
}}