网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.邮件发送程序(可以同时发给多个.
.Windows2000下安装J2EE和部署J2E.
.DHCP(动态主机配置协议)基本信.
.一个根据筛选法求出100以内的所有.
.Java的一些关键特性.
.课程介绍(7):SEM-SL-345 Java 2平.
.SCSA Exam Questions 06/10/02.
.谈谈Java语言的垃圾收集器.
.在Java中保留Stereotype.
.利用JMS和JCA实现应用之间的解偶.
.Java的简单数据类型.
.GetFile 方法.
.如何用Java得到硬盘空间.
.EJB轻松进阶之八.
.JBuilder 2005开发Applet游戏全接.
.用 IBM Holosofx 进行业务流程管.
.unshift 方法.
.B/S系统权限控制的一种简单方法.
.Java 语法介绍(一):Java语言的基.
.Java API 中文版.

JAVA静态变量

发表日期:2008-1-5



  Java的静态变量相当于类字段,而不用理解为对象字段。



--------------------------------------------------------------------------------
Static Fields and Methods
In all sample programs that you have seen, the main method is tagged with the static modifier. We are now ready to discuss the meaning of this modifier.

Static Fields
If you define a field as static, then there is only one sUCh field per class. In contrast, each object has its own copy of all instance fields. For example, let's suppose we want to assign a unique identification number to each employee. We add an instance field id and a static field nextId to the Employee class:

class Employee
{
. . .
private int id;
private static int nextId = 1;
}

Now, every employee object has its own id field, but there is only one nextId field that is shared among all instances of the class. Let's put it another way. If there are one thousand objects of the Employee class, then there are one thousand instance fields id, one for each object. But there is a single static field nextId. Even if there are no employee objects, the static field nextId is present. It belongs to the class, not to any individual object.


In most object-oriented programming languages, static fields are called class fields. The term "static" is a meaningless holdover from C++.



Let's implement a simple method:

public void setId()
{
id = nextId;
nextId++;
}

Suppose you set the employee identification number for harry:

harry.setId();

Then the id field of harry is set, and the value of the static field nextId is incremented:

harry.id = . . .;
Employee.nextId++;

Constants
Static variables are quite rare. However, static constants are more common. For example, the Math class defines a static constant:

public class Math
{
. . .
public static final double PI = 3.14159265358979323846;
. . .
}

You can Access this constant in your programs as Math.PI.

If the keyWord static had been omitted, then PI would have been an instance field of the Math class. That is, you would need an object of the Math class to access PI, and every object would have its own copy of PI.

Another static constant that you have used many times is System.out. It is declared in the System class as:
上一篇:java接口用法学习----------------java菜鸟学飞第一步 人气:834
下一篇:Java开源工具系列(三)Struts 人气:584
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐