关键字static

  • 静态成员属于类本身的,而不是属于对象,被类的所有对象所共有
  • 即使不创建对象,也可以使用类本身的静态成员
  • 静态成员分为
  • 静态数据成员
  • 静态方法成员
  • 使用静态成员的两种方法
  • 类名.静态成员名
  • 类对象名.静态成员名
  • 编写使用静态变量统计一个类产生的实例对象的个数的程序
//程序证明static属性i属于类本身,创建对象的时候不再单独创建数据成员i的内存
class A
{
    public static int i = 10;
    public void show()
    {
        System.out.printf("%d\n",i);
    }
}
class Teststatic_1
{
    public static void main(String[] args)
    {
        A aa1 = new A();
        A aa2 = new A();
        A aa3 = new A();

        aa1.i = 20;
        aa2.show();
        System.out.printf("%d\n",aa3.i);
    }
}

程序运行示例:
——————————————————————————————
20
20
——————————————————————————————
//static属性i是属于类本身,或者讲:没有对象,我们仍然可以直接通过类名的方式访问该类内部的static属性
            //static方法 同理
class A{
    public static int i = 10;

    public static void f()
    {
        System.out.printf("%d\n",A.i);
    }

}
class Teststatic_2
{
    public static void main(String[] args)
    {
        System.out.printf("%d\n",A.i);
        A.f();
    }
}
程序运行示例:
——————————————————————————————
10
10
——————————————————————————————
//static属性和方法虽然属于类本身,虽然可以通过类名的方式访问
//但是static属性和方法很明显也属于类对象,当然也可以类对象名的方式进行访问
class A{
    public static int i = 10;

    public static void f()
    {
        System.out.printf("%d\n",A.i);
    }

}
class Teststatic_3
{
    public static void main(String[] args)
    {
        // System.out.printf("%d\n",A.i);
        // A.f();
        A aa = new A();
        aa.f();
        System.out.printf("%d\n",aa.i);
    }
}

程序运行示例:
——————————————————————————————
10
10
——————————————————————————————
//只有非private的static成员才可以通过类名的方式访问
//static只是表明了该成员具有了可以通过类名访问的潜在特征
//但是否可以通过类名访问,还必须满足一个条件,该成员必须是非private
class A{
    public static int i = 10;

    private static void f()
    {
        System.out.printf("%d\n",A.i);
    }

}
class Teststatic_4
{
    public static void main(String[] args)
    {
        // System.out.printf("%d\n",A.i);   OK
        A.f();
        // A aa = new A();  
        // aa.f();      OK
        // System.out.printf("%d\n",aa.i);  OK
    }
}

程序运行示例:
——————————————————————————————
Teststatic_4.java:18: 错误: f() 在 A 中是 private 访问控制
        A.f();
         ^
1 个错误
______________________________
//说明静态方法不能访问非静态成员
//说明非静态方法可以访问静态成员
class A{
    private  static int i = 10;
    public int j = 99;

    public static void f()
    {
        //g();  //error 静态方法不能访问非静态成员
        // j = 22; //error 静态方法不能访问非静态成员
        System.out.printf("FFFF\n");
    }
    public void g()
    {
        f();      //OK 说明非静态方法可以访问静态成员
        System.out.printf("GGGG\n");
        System.out.printf("%d\n",i);
    }

}
class Teststatic_5
{
    public static void main(String[] args)
    {
        A aa = new A();  
        aa.g();      
        // System.out.printf("%d\n",aa.i);  OK
    }
}

程序运行示例:
——————————————————————————————
FFFF
GGGG
10
______________________________
class A
{
    public int i;
    private static int cnt = 0;

    public A()
    {
        cnt++;
    }
    public A(int i)
    {
        this.i = i;
        cnt++;      //this.cnt -->error  静态属于类,故没有this指针
    }

    public static int getCnt()
    {
        return cnt;
    }


}

class M
{
    public static void main(String[] args)
    {
        A aa1 = new A();
        A aa2 = new A();
        System.out.printf("%d\n",A.getCnt());
    }
}

程序运行示例:
——————————————————————————————
2
______________________________
  • 在静态方法里只能直接调用同类中其他的静态成员(包括变量和方法),而不能直接访问类中的非静态成员。这是因为,对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象
  • 静态方法不能以任何方式应用this和super关键字(super关键字在下一章讲解)。与上面的道理一样,因为静态方法在引用的对象根本就没有产生。
  • 静态方法只能访问类的静态成员。
    但非静态方法却可以访问类中所有成员,包括静态成员