Core Java Volume I — 4.4. Static Fields and Methods
4.4. 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.
4.4.1. Static Fields(静态域)
If you define a field as static, then there is only one such field per class(如果一个域是static的,那么它对于一个类来说只有一份拷贝). 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 static int nextId = 1;
private int id;
. . .
}
Every employee object now 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 1,000 objects of the Employee class, then there are 1,000 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(即使没有创建实例,static域都是存在的,static域属于class??).
Note
In some 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 to the current value of the static field nextId, and the value of the static field is incremented:
harry.id = Employee.nextId;
Employee.nextId++;
4.4.2. Static 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 this class to access PI, and every Math 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 follows:
public class System
{
. . .
public static final PrintStream out = . . .;
. . .
}
As we mentioned several times, it is never a good idea to have public fields, because everyone can modify them. However, public constants (that is, final fields) are fine.
Since out has been declared as final, you cannot reassign another print stream to it:
System.out = new PrintStream(. . .); // ERROR--out is final
Note
If you look at the System class, you will notice a method setOut that sets System.out to a different stream. You may wonder how that method can change the value of a final variable. However, the setOut method is a native method, not implemented in the Java programming language. Native methods can bypass the access control mechanisms of the Java language. This is a very unusual workaround that you should not emulate in your programs.
4.4.3. Static Methods(静态方法)
Static methods are methods that do not operate on objects. For example, the pow method of the Math class is a static method. The expression
Math.pow(x, a)
computes the power xa. It does not use any Math object to carry out its task. In other words, it has no implicit parameter.
You can think of static methods as methods that don't have a this parameter. (In a nonstatic method, the this parameter refers to the implicit parameter of the method—see Section 4.3.5, “Implicit and Explicit Parameters,” on p. 152.)
Since static methods don't operate on objects, you cannot access instance fields from a static method(静态方法不能操作对象,不能在一个静态方法中访问实例域). However, static methods can access the static fields in their class(静态方法可以访问类中的静态域). Here is an example of such a static method:
public static int getNextId()
{
return nextId; // returns static field
}
To call this method, you supply the name of the class:
int n = Employee.getNextId();
Could you have omitted the keyword static for this method? Yes, but then you would need to have an object reference of type Employee to invoke the method.
Note
It is legal to use an object to call a static method. For example, if harry is an Employee object, then you can call harry.getNextId() instead of Employee.getnextId(). However, we find that notation confusing. The getNextId method doesn't look at harry at all to compute the result. We recommend that you use class names, not objects, to invoke static methods.
Use static methods in two situations(以下两种情况下使用静态方法):
- When a method doesn't need to access the object state because all needed parameters are supplied as explicit parameters (example: Math.pow).
- When a method only needs to access static fields of the class (example: Employee.getNextId).
C++ Note
Static fields and methods have the same functionality in Java and C++. However, the syntax is slightly different. In C++, you use the :: operator to access a static field or method outside its scope, such as Math::PI. The term “static” has a curious history. At first, the keyword static was introduced in C to denote local variables that don't go away when a block is exited. In that context, the term “static” makes sense: The variable stays around and is still there when the block is entered again. Then static got a second meaning in C, to denote global variables and functions that cannot be accessed from other files. The keyword static was simply reused, to avoid
introducing a new keyword. Finally, C++ reused the keyword for a third, unrelated, interpretation—to denote variables and functions that belong to a class but not to any particular object of the class. That is the same meaning the keyword has in Java.
4.4.4. Factory Methods(工厂方法??)
Here is another common use for static methods. The NumberFormat class uses factory methods that yield formatter objects for various styles.
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
NumberFormat percentFormatter = NumberFormat.getPercentInstance();
double x = 0.1;
System.out.println(currencyFormatter.format(x)); // prints $0.10
System.out.println(percentFormatter.format(x)); // prints 10%
Why doesn't the NumberFormat class use a constructor(构造函数) instead? There are two reasons:
- You can't give names to constructors. The constructor name is always the same as the class name. But we want two different names to get the currency instance and the percent instance.
- When you use a constructor, you can't vary the type of the constructed object. But the factory methods actually return objects of the class DecimalFormat, a subclass that inherits from NumberFormat. (See Chapter 5 for more on inheritance.)
4.4.5. The main Method(main方法)
Note that you can call static methods without having any objects(调用静态方法不需要通过对象). For example, you never construct any objects of the Math class to call Math.pow.
For the same reason, the main method is a static method.
public class Application
{
public static void main(String[] args)
{
// construct objects here
. . .
}
}
The main method does not operate on any objects. In fact, when a program starts, there aren't any objects yet. The static main method executes, and constructs the objects that the program needs.
Tip
Every class can have a main method. That is a handy trick for unit-testing of classes. For example, you can add a main method to the Employee class:
class Employee
{
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month-1, day);
hireDay = calendar.getTime();
}
. . .
public static void main(String[] args) // unit test
{
Employee e = new Employee("Romeo", 50000, 2003, 3, 31);
e.raiseSalary(10);
System.out.println(e.getName() + " " + e.getSalary());
}
. . .
}
If you want to test the Employee class in isolation, simply execute
java Employee
If the employee class is a part of a larger application, then you start the application with
java Application
and the main method of the Employee class is never executed.
The program in Listing 4.3 contains a simple version of the Employee class with a static field nextId and a static method getNextId. We fill an array with three Employee objects and then print the employee information. Finally, we print the next available identification number, to demonstrate the static method.
Note that the Employee class also has a static main method for unit testing. Try running both
java Employee
and
java StaticTest
to execute both main methods.
public class StaticTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3]; staff[0] = new Employee("Tom", 4000);
staff[1] = new Employee("Dick", 6000);
staff[2] = new Employee("Harry", 6500); for(Employee e : staff)
{
e.setId();
System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary());
} int n = Employee.getNextId(); System.out.println("Next availabe id = " + n);
}
} class Employee
{
private static int nextId = 1;
private String name;
private double salary;
private int id; public Employee(String n, double s)
{
name = n;
salary = s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
public void setId()
{
id = nextId;
nextId++;
}
public static int getNextId()
{
return nextId;
}
public static void main(String[] args)
{
Employee e = new Employee("Harry", 50000);
System.out.println(e.getName() + " " + e.getSalary());
}
}
Core Java Volume I — 4.4. Static Fields and Methods的更多相关文章
- Core Java Volume I — 4.7. Packages
4.7. PackagesJava allows you to group classes in a collection called a package. Packages are conveni ...
- Core Java Volume I — 1.2. The Java "White Paper" Buzzwords
1.2. The Java "White Paper" BuzzwordsThe authors of Java have written an influential White ...
- Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses
5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...
- Core Java Volume I — 4.6. Object Construction
4.6. Object ConstructionYou have seen how to write simple constructors that define the initial state ...
- Core Java Volume I — 4.5. Method Parameters
4.5. Method ParametersLet us review the computer science terms that describe how parameters can be p ...
- Core Java Volume I — 3.10. Arrays
3.10. ArraysAn array is a data structure that stores a collection of values of the same type. You ac ...
- Core Java Volume I — 3.8. Control Flow
3.8. Control FlowJava, like any programming language, supports both conditional statements and loops ...
- Core Java Volume I — 4.10. Class Design Hints
4.10. Class Design HintsWithout trying to be comprehensive or tedious, we want to end this chapter w ...
- Core Java Volume I — 4.1. Introduction to Object-Oriented Programming
4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is th ...
随机推荐
- [转]Windows的窗口刷新机制
1.Windows的窗口刷新管理 窗口句柄(HWND)都是由操作系统内核管理的,系统内部有一个z-order序列,记录着当前窗口从屏幕底部(假象的从屏幕到眼睛的方向),到屏幕最高层的一个窗口句柄的排序 ...
- [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想
Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...
- 二分图 最大权匹配 km算法
这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...
- 谈谈CSS的布局,display、position、float
前言 前端一直是我的一个很大的缺憾,这段时间痛顶思痛,决定好好的把前台的东西加强,这不,在学习了一段时间js之后,在做一些小练习,却发现最基本的一些css知识却还不了解,所以便有了这篇博文. 块级元素 ...
- linux :TOP命令及参数解析
第二行 分别显示:total进程总数. running正在运行的进程数. sleeping睡眠的进程数.stopped停止的进程数. zombie僵尸进程数. 第三行 分别显示: %us 用户空间占用 ...
- [css3]文字过多以省略号显示
text-overflow:ellipsis; 优点: 1.不用通过程序限定字数 2.有利于SEO(实际上并未被截字,只是局限于宽度未被显示而已) width: 某个值; overflow: hidd ...
- [Js]布局转换
为什么要布局转换? 要这样的效果,单写css,只要给每个li浮动就行,不需要绝对定位.但是比如做一些效果(如鼠标移入图片变大),就需要改变位置了.直接给每个li在css上定好位置不方便,也不知道有几个 ...
- 利用dispatch_once创建单例
无论是爱还是恨,你都需要单例.实际上每个iOS或Mac OS应用都至少会有UIApplication或NSApplication. 什么是单例呢?Wikipedia是如此定义的: 在软件工程中,单例 ...
- for循环语句示例应用
age = 22 #优化前 ''' for i in range(10): guess_num = int(input('input your guess num:')) if guess_num = ...
- C#Base64编码
一. Base64的编码规则 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24 位数据,再把这24位数据 ...