静态类(static class)即定义了静态方法,静态变量,静态代码块或者内部静态类的类。这些静态成员不需要实例化即可直接引用。
- 静态方法:不需要实例化,可直接引用。
- 静态变量:不需要实例化,可直接引用。
- 静态代码块:在系统初始化时时使用
- 静态内部类:不能操作访问外部数据。
静态的类在运行时加载到内存中,不需要实例化,在类的内部也不能使用this。
1. 在类中生命一个方法为static,可以直接点用词方法,而不需要对该类进行实例化,调用格式为“类名.静态方法名”
2.如果在类中定义了静态变量(static field),在类装载时,只分配一块存储空间。也就是说对于该类的所有对象,都是在操作同一块存储空间。示例代码如下:
(1) StaticClass.java文件
package com.chensl.staticTest;
public class StaticClass {
public static int i = 0;
public static int j = 0;
public static void addTest(){
i++;
}
}
(2)StaticClassTest.java文件
package com.chensl.staticTest;
public class StaticClassTest {
public static void main(String[] args) {
StaticClass sc1,sc2;
sc1= new StaticClass();
sc2= new StaticClass();
System.out.println("sc1.i = "+sc1.i+" sc2.i = "+sc2.i);
System.out.println("sc1.j = "+sc1.j+" sc2.j = "+sc2.j);
sc1.i++;
sc1.j--;
System.out.println("sc1.i = "+sc1.i+" sc2.i = "+sc2.i);
System.out.println("sc1.j = "+sc1.j+" sc2.j = "+sc2.j);
System.out.println("StaticClass.i = "+StaticClass.i+" StaticClass.j = "+StaticClass.j);
}
}
3.静态代码块
静态代码块有点类似于C中的全局变量的概念,修改上面示例代码如下:
(1)StaticJava.java文件
package com.chensl.staticTest;
public class StaticClass {
public static int i = 0;
public StaticClass(){
i=15;
}
public StaticClass(int n){
i=n;
}
public static void addTest(){
i++;
}
}
(2)StaticJavaTest.java文件
package com.chensl.staticTest;
public class StaticClassTest {
StaticClass sc=new StaticClass(10);
static StaticClass sc1,sc2;
//静态代码块在系统启动时自动加载执行
static{
System.out.println("初始化:sc1.i= "+sc1.i +" sc2.i= "+ sc2.i);
sc1 = new StaticClass(27);
System.out.println("初始化:sc1.i= "+sc1.i +" sc2.i= "+ sc2.i);
sc1 = new StaticClass(15);
System.out.println("初始化:sc1.i= "+sc1.i +" sc2.i= "+ sc2.i);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StaticClassTest test = new StaticClassTest();
System.out.println("sc1.i= "+sc1.i);
System.out.println("sc1.i= "+sc1.i +" sc2.i= "+ sc2.i);
StaticClass.addTest();
System.out.println("sc1.i = "+sc1.i+" StaticClass.i = "+StaticClass.i);
}
}
注,以上有些字段的访问可以不用声明对象,而直接使用静态类名即可
4.静态内部类
static 不但可以添加到字段、方法、静态模块中,还可以添加到类名称前,将类声明为静态的,不过普通类不可以声明为static,只有内部类才可以声明成static,这时,这个声明的静态内部类可以直接使用,而不需要实例化外部类。
使用内部类可以把一个类隐藏在另外一个类的内部,且不需要内部类引用外围类的对象。声明在接口中的内部类,自动成为static和public
简单静态内部类示例:
package com.chensl.staticTest;
public class InnerClassTest {
//定义一个内部静态类
public static class InnerClass{
InnerClass(){
System.out.println("InnerClass");
}
public void print(String string){
System.out.println(string);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//引用静态内部类
InnerClassTest.InnerClass IC = new InnerClassTest.InnerClass();
IC.print("this is a test");
}
}
输出结果为:
InnerClass
this is a test
典型静态内部类范例:计算数组中最小值和最大值的问题。
package com.chensl.staticTest;
/**
* This program demonstrates the use of static inner classes.
* @version 1.01 2004-02-27
* @author Cay Horstmann,添加部分注释by_村夫
*/
public class StaticInnerClassTest
{
public static void main(String[] args)
{
double[] d = new double[20];
for (int i = 0; i < d.length; i++){
d[i] = 100 * Math.random();
System.out.println("d["+i+"]= "+d[i]);
}
ArrayAlg.Pair p = ArrayAlg.minmax(d);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
}
}
class ArrayAlg
{
/**
* 内部静态类,在内部类不需要访问外围对象的时候,应该使用静态内部类,
* 有的使用嵌套类(nested class)表示静态内部类
* A pair of floating-point numbers
*/
public static class Pair
{
/**
* Constructs a pair from two floating-point numbers
* @param f the first number
* @param s the second number
*/
public Pair(double f, double s)
{
first = f;
second = s;
}
/**
* Returns the first number of the pair
* @return the first number
*/
public double getFirst()
{
return first;
}
/**
* Returns the second number of the pair
* @return the second number
*/
public double getSecond()
{
return second;
}
private double first;
private double second;
}
/**
* 通过使用静态内部类,经过一次遍历,可以找到最大最小值两个结果,并返回。如果只使用方法,
* 一次只能返回一个类,所以需要遍历两次数组,才能找到最大值和最小值
* Computes both the minimum and the maximum of an array
* @param values an array of floating-point numbers
* @return a pair whose first element is the minimum and whose second element
* is the maximum
*/
public static Pair minmax(double[] values)
{
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (double v : values)
{
if (min > v) min = v;
if (max < v) max = v;
}
return new Pair(min, max);
}
}