static
static是Java中的非访问修饰符,适用于块、变量、方法、内部类。
要想创建一个静态成员(块、变量、方法、内部类),需在其声明之前加上 static 关键字。当一个成员被声明为 static,当成员声明为static时,可以在创建其类的任何对象之前访问该成员,并且不需要引用任何对象。例如,在下面的Java程序中,我们访问静态方法m1(),而不创建Test类的任何对象。
//在一个类实例化之前可以访问一个静态成员
class Test {
static void m1 () {
System.out.println("from m1");
}
public static void main(String[] args) {
m1();
}
}
输出:
from m1
static代码块
如果你需要做计算初始化静态变量,你可以声明一个 static 代码块,当类加载的时候,这个代码块只会执行一次。下面的java程序演示了如果使用 static 代码块。
//使用静态代码块的Java程序
class TestOne
{
// static variable
static int a = 10;
static int b;
// static block
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String[] args)
{
System.out.println("from main");
System.out.println("Value of a : "+a);
System.out.println("Value of b : "+b);
}
}
输出:
from main
Value of a : 10
Value of b : 40
有关static块的文章,请参考 static blocks
static变量
当变量声明为 static 时,将创建变量的单一副本,并在类级别上在所有对象之间共享。静态变量实际上是全局变量,所有类的实例共享相同的静态变量。
静态变量的重点
- 我们只可以在 类级别 创建静态变量,详见 这里
- 静态代码块和静态变量按照它们在程序中出现的顺序执行
下面的Java程序演示了静态代码块和静态变量按照它们在程序中出现的顺序执行。
// Java程序演示了静态代码块和静态变量按照它们在程序中出现的顺序执行
class Test
{
// static variable
static int a = m1();
// static block
static {
System.out.println("Inside static block");
}
// static method
static int m1() {
System.out.println("from m1");
return 20;
}
// static method(main !!)
public static void main(String[] args)
{
System.out.println("Value of a : "+a);
System.out.println("from main");
}
}
Output:
from m1
Inside static block、
Value of a : 20
from main
static方法
当一个方法用static关键字声明时,它被称为静态方法。静态方法最常见的例子就是main()方法。如上述所述,任何静态方法都可以在其类的任何对象创建之前访问,并且不需要引用任何对象。声明为静态方法有以下几个限制:
- 它们只能直接访问其他静态方法
- 它们只能直接方法静态数据
- 它们不能以任何方式访问this或者super
下面Java程序演示了静态方法的限制。
class TestS
{
//static变量
static int a = 10;
//实例变量
int b = 20;
// 静态方法
static void m1()
{
a = 20;
System.out.println("from m1");
// 不能对非静态字段b进行静态引用
b = 10; //编译错误
// 不能对非静态方法m2()进行静态引用
m2(); // 编译错误
//不能在静态上下文中引用super
System.out.println(super.a); // 编译错误
}
//实例方法
void m2()
{
System.out.println("from m2");
}
public static void main(String[] args)
{
// main method
}
}
什么时候使用static变量和方法?
对所有对象都是公用的属性使用 static 变量。例如,在Student类中,所有的 学生对象都共享相同的变量 college name 。使用静态方法更变静态变量。
思考下面Java程序,它演示了在变量和方法上使用static关键字。
class Student
{
String name;
int rollNo;
// 静态变量
static String collegeName;
// static counter to set unique roll no
static int counter = 0;
public Student(String name)
{
this.name = name;
this.rollNo = setRollNo();
}
// getting unique rollNo
// through static variable(counter)
static int setRollNo()
{
counter++;
return counter;
}
// static method
static void setCllg(String name){
collegeName = name ;
}
// instance method
void getStudentInfo(){
System.out.println("name : " + this.name);
System.out.println("rollNo : " + this.rollNo);
// accessing static variable
System.out.println("cllgName : " + collegeName);
}
}
//Driver class
public class StaticDemo
{
public static void main(String[] args)
{
// calling static method
// without instantiating Student class
Student.setCllg("XYZ");
Student s1 = new Student("Alice");
Student s2 = new Student("Bob");
s1.getStudentInfo();
s2.getStudentInfo();
}
}
Output
name : Alice
rollNo : 1
collegeName : XYZ
name : Bob
rollNo : 2
collegeName : XYZ
静态内部类 :我们不能用static修饰符修饰顶层类,但是可以用statci修饰内部类。这种类称为内部静态类。对于静态内部类,请参见Java中的 静态内部类.