java中的static关键字分析

    在java中,一切都是对象,每个对象都属于一个类,一个类有数据域和方法。在程序中,可以使用一个类的多个引用来使用本类的多个对象。这些不同的对象的数据域有各自的内存空间。但是,如果一个类的数据域是static的,那么,这个数据域将属于本类所有,本类的所有的对象将共同使用一块内存空间,所以,static的变量只会初始化一次。
    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
==================

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值