static类型的静态数据域的几种定义方式:
方式1:
private static int classNo1; //这种方式的变量会被初始化为0或者null
方式2:
private static int classNo2 = 20; //这种方式的变量就被初始化为指定的值了。
方式3:
private static int classNo3;
static{
classNo3 = 30; //这是另外一种初始化方式。效果同方式2。
}
一个类中,static方法就是没有this的方法,一个类,在没有定义对象的前提下,它可以访问本类中的static变量,但是不能访问本类中的非static的变量。这是因为,在对象尚未定义的时候,类的非static变量还没有申请内存空间,static变量却已经有了内存空间。
按照上面的分析,假设一个类中的变量是static的,一个类中的方法全部是static的,那么这个Java程序就不要对象就可以运行,程序就“演变为”类似与C语言那种面向结构的程序了:一个static方法调用另外一个static方法,static变量就是这个程序的全局变量。
另外,与C++不同,一个类的方法中,不管是static方法,还是非static方法,里面都不能再定义static的局部变量或者代码块。
代码举例说明:
package com.baby.study.javaStudy.tij4;
/**
* @author Baby
* @version 1.0
*
*/
public class StaticTest {
private static int ClassNo1;
private static int ClassNo2 = 20;
private static int ClassNo3;
private int otherNumber;
static {
System.out.println("静态数据成员,属于类所共有,只会执行一次");
ClassNo3 = 30;
}
{
System.out.println("非静态数据成员是属于对象的,只要有对象生成,他就会执行。\t" + this);
otherNumber = 100;
}
public StaticTest() {
super();
System.out.println(this);
System.out.println("static int ClassNo = " + StaticTest.ClassNo1);
System.out.println("static int ClassNo2 = " + StaticTest.ClassNo2);
System.out.println("static int ClassNo3 = " + StaticTest.ClassNo3);
System.out.println("int otherNumber = " + otherNumber);
}
public static int getClassNo1() {
return ClassNo1;
}
public static void increMentClassNo() {
ClassNo1++;
ClassNo2++;
ClassNo3++;
}
public int getOtherNumber() {
return otherNumber;
}
public void setOtherNumber(int otherNumber) {
//static final int a =10;
this.otherNumber = otherNumber;
}
/**
* @param args
*/
public static void main(String[] args) {
/*
* otherNumber不是static型的,他属于某一个对象,在没有对象存在的情况下,变量将不能被访问。
* 这也是otherNumber不能用在static方法中的原因。
*/
// System.out.println("otherNumber is "+otherNumber);
// 不要对象也可以访问static变量
System.out.println("default value of static int classNo is " + StaticTest.ClassNo1);
System.out.println("value of static int ClassNo2 is " + StaticTest.ClassNo2);
StaticTest staticTest1 = new StaticTest();
StaticTest.increMentClassNo();
System.out.println("StaticTest.getClassNo1() is " + StaticTest.getClassNo1());
// 使用以下的方式也可以访问静态数据成员。但是eclipse会有告警,最好的方式就是使用类名来访问。
staticTest1.ClassNo1++;
staticTest1.ClassNo2++;
System.out.println("staticTest1.getClassNo1() is " + staticTest1.getClassNo1());
System.out.println("staticTest1.ClassNo2 is " + staticTest1.ClassNo2);
StaticTest.ClassNo3++;
System.out.println("staticTest1.ClassNo3 is " + StaticTest.ClassNo3);
StaticTest staticTest2 = new StaticTest();
System.out.println("staticTest1.getOtherNumber() is " + staticTest2.getOtherNumber());
//System.out.println("==================");
//static StaticTest staticTest3 = new StaticTest();
}
}
程序的处理结果:
静态数据成员,属于类所共有,只会执行一次
default value of static int classNo is 0
value of static int ClassNo2 is 20
非静态数据成员是属于对象的,只要有对象生成,他就会执行。 com.baby.study.javaStudy.tij4.StaticTest@139a55
com.baby.study.javaStudy.tij4.StaticTest@139a55
static int ClassNo = 0
static int ClassNo2 = 20
static int ClassNo3 = 30
int otherNumber = 100
StaticTest.getClassNo1() is 1
staticTest1.getClassNo1() is 2
staticTest1.ClassNo2 is 22
staticTest1.ClassNo3 is 32
非静态数据成员是属于对象的,只要有对象生成,他就会执行。 com.baby.study.javaStudy.tij4.StaticTest@1db9742
com.baby.study.javaStudy.tij4.StaticTest@1db9742
static int ClassNo = 2
static int ClassNo2 = 22
static int ClassNo3 = 32
int otherNumber = 100
staticTest1.getOtherNumber() is 100
==================