静态常量
public final class Math {
/**
* Don't let anyone instantiate this class.
*/
private Math() {}
/**
* The {@code double} value that is closer than any other to
* e, the base of the natural logarithms.
*/
public static final double E = 2.7182818284590452354;
/**
* The {@code double} value that is closer than any other to
* pi, the ratio of the circumference of a circle to its
* diameter.
*/
public static final double PI = 3.14159265358979323846;
在Math类中定义了一个静态常量,在程序中采用Math.PI的形式获得这个常量。
public final class System {
public final static PrintStream out = null;
另一个多次使用的静态常量是System.out。
因为out被声明为final,所以,不允许再将其他打印流赋给它。
静态方法
静态方法是一种不能向对象实施操作的方法。
例如,Math类的pow方法就是一个静态方法。
可以认为静态方法是没有this参数的方法。
public final class Math {
public static double pow(double a, double b) {
return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}
main方法不对任何对象操作。事实上,再启动程序时还没有任何一个对象。静态的main方法将执行并创建程序所需要的对象。
注释
在局绝大多数的面向对象程序设计语言中,静态域被称为类域。术语" static "只是沿用了C++的叫法,并无实际意义。