static成员属于类而非实例,随类加载初始化且仅一份副本;所有实例共享static变量,static方法无需对象调用;不可在static上下文中访问this或非static成员;子类可继承但不能重写static成员,只能隐藏;初始化顺序为父类static块→子类static块→父类实例块→父类构造器→子类实例块→子类构造器;static块仅执行一次,适合复杂类级初始化;静态内部类不持外部类引用,适用于延迟加载单例;static import易引发命名冲突,应避免通配符;static成员生命周期绑定类加载器,多ClassLoader加载会导致多份副本。
Java中static成员被加载到方法区(JDK 8+为元空间),随类加载而初始化,且只有一份副本。所有实例共享同一
份static变量,调用static方法也不依赖对象实例。
常见错误:在static方法中直接访问this或非static成员——编译报错non-static variable xxx cannot be referenced from a static context。
static变量在类首次主动使用时初始化(如第一次new、第一次调用static方法、第一次读写static字段)public或protected的static成员,但不能“重写”——只能隐藏(用同签名static方法定义,不触发多态)static块 → 子类static块 → 父类实例块 → 父类构造器 → 子类实例块 → 子类构造器static代码块在类加载时执行,且仅执行一次。它比static变量声明更灵活,可用于复杂初始化逻辑(如加载配置、注册驱动、预热缓存)。
注意:多个static块按源码顺序依次执行;若抛出异常,类加载失败,后续无法使用该类。
public class ConfigLoader {
public static final Map CONFIG;
static {
try {
Properties props = new Properties();
props.load(ConfigLoader.class.getResourceAsStream("/app.properties"));
CONFIG = Collections.unmodifiableMap((Map) props);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
}
普通内部类隐式持有外部类实例引用,而static内部类没有该引用。因此,static内部类常被用于实现延迟加载且线程安全的单例(“Initialization-on-demand holder idiom”)。
优势:无同步开销,由JVM类加载机制保证线程安全,且不依赖volatile或synchronized。
static成员(否则编译报错)static成员,包括static块和static方法static块执行public class Singleton {
private Singleton() {}
private static class Holder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return Holder.INSTANCE;
}
}
import static导入的是类的static成员(字段或方法),让调用方省略类名前缀。但它会污染当前命名空间,尤其当多个static import引入同名方法时,编译失败。
典型问题:同时import static java.lang.Math.*和import static org.junit.Assert.*,再写assertEquals(...)——编译器无法确定调用哪个。
import static java.util.Collections.singletonList;
static import有特殊高亮,留意是否覆盖了预期方法ClassLoader加载两次,就会产生两份独立的static变量——这在OSGi、Spring Boot DevTools、热部署场景下极易引发状态不一致问题。