十 java面向对象程序设计(static关键字)
/**
* 面向对象之五
* static关键字总结
*/
class Static
{
/**
* 1,静态变量:
* a,在变量前面加上static关键字后,该变量为静态变量,
* 这时,无论是否产生了对象或无论产生了多少对象的情况下,这个特定的变量在内存空间里只有一份
* b,静态的变量在调用时,可以不用产生对象,直接通过"类名.变量名"这种形式进行调用.
*/
static int count1 = 0;//定义一个静态变量用于计数
int count2 = 0;//定义一个非静态变量,用于对比
public Static()
{
System.out.println("new Static()");
System.out.println("static var: " + (++count1));//每次创建该类对象时,静态变量开始计数
System.out.println("no static var: " + (++count2));//添加一个非静态变量,同样用于计数,对比效果.
}
/**
* 2,静态方法
* a,和静态变量相同,静态方法同样可以在不创建对象下,通过类名直接调用.
* b,在静态方法里只能直接调用同类中其它的静态成员(包括变量和方法),而不能直接访问类中的非静态成员。
* 这是因为,对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象。
* c,静态方法不能以任何方式引用this和super关键字,因为静态方法在使用前不用创建任何实例对象,
* 当静态方法被调用时,this所引用的对象根本就没有产生。
*/
public static void getInfo()//静态方法,getInfo()
{
System.out.println("equals static function & no static function");
}
public void getInfo(int count1,int count2)//动态方法,重载getInfo()
{
Static.count1 = count1;
//this.count1 = count1;//也可以通过this调用静态变量count1
this.count2 = count2;
getInfo();//在非静态方法中可以直接调用静态方法.
}
public void function()
{
System.out.println("function is calling...");
}
public static void function(int count1)
{
Static.count1 = count1;
//function();//错误,不能在静态方法中调用非静态的方法function();
new Static().function();
}
public static void function(int count1,int count2)
{
function(count1);//但是,在静态方法中可以直接调用静态方法,和静态变量.
//this.count2 = count2;
//错误,静态方法中没有this和super,因为this是指向对象的,但静态方法在使用时根本无需产生对象,
//所以this无法引用这个没有产生的对象
Static s = new Static();
s.count2 = count2;//静态方法不能访问非静态的成员变量,所以必须先产生对象.
}
//总结:静态变量在内存中只有一份,静态方法和静态变量一样,在使用前不用创建任何实例对象,通过类名直接调用.
// 所以静态方法中没有this和super关键字.
// 静态方法不可以访问非静态的变量和方法,但是非静态方法可以访问静态的方法和变量.
/**
* 3,main()方法:
* main() 方法是静态的,因此JVM在执行main方法时不创建main方法所在的类的实例对象,
* 因而在main()方法中,我们不能直接访问该类中的非静态成员,必须创建该类的一个实例对象后,
* 才能通过这个对象去访问类中的非静态成员
*/
public static void main(String[] args)
{
/*静态成员变量和动态成员变量的对比*/
System.out.println("静态成员变量和动态成员变量的对比");
new Static();
new Static();
new Static();
//创建3个对象.静态变量count1值为3,证明无论创建多少对象,静态方法只占用一份内存.
//但非静态方法count2值每次都是1,证明每当新建一个对象时,就新建了一个count2的副本.
/*静态成员变量调用时可以不用产生对象*/
System.out.println("静态成员变量调用时可以不用产生对象");
System.out.println(new Static().count1);
System.out.println(new Static().count2);
System.out.println(Static.count1);
//System.out.println(Static.count2);必须通过对象调用非静态成员变量
/*调用静态方法*/
System.out.println("调用静态方法");
Static s = new Static();
s.getInfo(10, 20);
Static.function(10, 20);
}
}
public class StaticTest {
/**
* 4,静态代码块
* 静态块经常用来进行类属性的初始化。
* 类中的静态代码块被自动执行,尽管我们产生了类的多个实例对象,但其中的静态代码块只被执行了一次。
*/
static int count1 = 0;
static int count2 = 0;
//分别定义两个静态变量用于计数.
static//静态代码块
{
count1++;
System.out.println("static code block is calling...");
}
{//非静态代码块
count2++;
System.out.println("code block is calling...");
}
public static void main(String[] args)
{
/*静态代码块*/
new StaticTest();
new StaticTest();
new StaticTest();//new了3个对象,静态代码块只执行一次,而非静态代码块执行了3次.
System.out.println(count1);
System.out.println(count2);
}
}
/* output from Static main()
静态成员变量和动态成员变量的对比
new Static()
static var: 1
no static var: 1
new Static()
static var: 2
no static var: 1
new Static()
static var: 3
no static var: 1
静态成员变量调用时可以不用产生对象
new Static()
static var: 4
no static var: 1
4
new Static()
static var: 5
no static var: 1
1
5
调用静态方法
new Static()
static var: 6
no static var: 1
equals static function & no static function
new Static()
static var: 11
no static var: 1
function is calling...
new Static()
static var: 12
no static var: 1
*/
/* output from StaticTest main()
static code block is calling...
code block is calling...
code block is calling...
code block is calling...
1
3
*/