static关键字详解

embedded/2024/9/23 21:51:57/

文章目录

static_2">static

static是Java中的一个关键字,用于定义类级别的成员,类级别的成员是指那些属于整个类,而不是特定对象实例的成员。在Java中,类级别的成员包括静态变量和静态方法。

java">public class Example {// 静态变量(类级别的成员)public static int count = 0;// 静态方法(类级别的成员)public static void incrementCount() {count++;}
}

因为静态成员属于整个类而不是特定实例,所以它们在所有实例之间共享,减少了内存消耗。例如,静态变量用于保存所有对象共享的状态,而静态方法可用于提供通用的工具函数,这些功能可以直接通过类名访问,无需实例化对象。

但是由于静态成员在类加载时被初始化并在整个应用程序运行期间存在,可能导致测试变得复杂,因为静态变量的状态会影响所有实例。此外,静态成员无法通过继承进行扩展,这限制了其灵活性。多个线程访问静态变量时,还需处理线程安全问题。

static使用场景包括工具类、常量和共享状态。例如,工具类中的静态方法,如java.lang.Math类的数学函数,可以通过类名直接访问,而无需实例化对象。常量通过public static final声明,使其在整个应用中保持一致,并方便访问。静态变量用于存储所有类实例共享的数据,如计数器,所有实例可以访问和修改相同的值。

使用示例

static关键字可以用于声明静态变量,这些变量在所有实例之间共享;定义静态方法,允许通过类名直接调用;以及创建静态块,用于在类加载时执行初始化操作。还可用于定义静态内部类,这些类与外部类的实例无关,但可以访问外部类的静态成员。

  • 静态变量:声明属于类的变量,而不是实例。所有对象共享同一个静态变量。
    java">public class Example {public static int count = 0;
    }
    
  • 静态方法:定义不依赖于实例的方法,可以直接通过类名调用。
    java">public class Example {public static void printMessage() {System.out.println("Hello, World!");}
    }
    
  • 静态块:用于在类加载时初始化静态变量,静态块在类加载时执行一次。
    java">public class Example {static {System.out.println("Static block executed");}
    }
    
  • 静态内部类:定义与外部类实例无关的内部类,静态内部类可以直接访问外部类的静态成员。
    java">public class OuterClass {static class StaticInnerClass {void display() {System.out.println("Static inner class");}}
    }
    

除了静态变量、静态方法、静态块和静态内部类这几种常见的使用方式外,static关键字还可用于静态导入和静态方法引用。

  • 静态导入:允许在代码中直接使用类的静态成员,无需使用类名。这可以使代码更简洁。
    java">import static static">java.lang.Math.*;public class Example {public static void main(String[] args) {double result = sqrt(25); // 直接使用 sqrt 方法,无需 Math.sqrt()System.out.println(result);}
    }
    
  • 静态方法引用:在lambda表达式或方法引用中,可以使用静态方法作为目标。这种用法使代码更具表达力。
    java">public class Example {public static int multiplyByTwo(int x) {return x * 2;}public static void main(String[] args) {Function<Integer, Integer> function = Example::multiplyByTwo;System.out.println(function.apply(5)); // 输出 10}
    }
    

static_82">static底层原理

在JVM中,静态变量、静态方法和静态代码块都存储在方法区。方法区是JVM的一部分,用于存储类的结构信息,包括类的元数据、静态变量、静态方法和常量池等。类加载时,JVM在方法区为这些静态成员分配内存,这块内存被所有类的实例共享,并在整个类的生命周期内保持不变,所以静态变量不需要为每个对象实例重新创建。

类加载的过程包括三个步骤:

  1. 加载:JVM通过类加载器读取.class文件的字节码,并将其加载到内存中的方法区;
  2. 连接:包括验证(确保字节码文件的正确性)、准备(为静态变量分配内存并赋初始值)、解析(将常量池中的符号引用转换为直接引用);
  3. 初始化:这是类加载机制的最后一步,在这个阶段,Java程序代码才开始真正执行,此阶段负责执行静态变量和执行静态块。初始化的时候才会为普通成员变量赋值,而在准备阶段已经为静态变量赋过一次值、静态方法已经初始过。也就是说如果我们在静态方法中调用非静态成员变量会超前,可能会调用了一个还未初始化的变量。因此编译器会报错。

静态变量在类加载时被初始化,并在方法区中分配一块内存。这块内存被所有类的实例共享,所有实例访问的是同一份静态变量,不需要为每个实例单独创建。静态方法也存在于方法区,可以通过类名直接调用,不需要创建类的实例。静态代码块在类加载时执行一次,用于初始化静态资源。静态成员与对象实例无关,它们的内存分配和初始化在类加载阶段完成,并在整个应用中保持一致。举个例子:

java">public class Example {public static int count = 0; // 静态变量
}

当类Example被加载时,count变量在方法区中被分配内存,并初始化为0。这段内存空间只存在一份,并且所有对Example.count的访问都指向这段内存空间。

静态初始化顺序

掌握静态变量、静态代码块和静态方法的加载顺序,有助于合理安排代码逻辑,解决因依赖关系引起的问题。对于调试复杂的类加载过程也很重要,可以更快地定位和解决问题。除此之外,这种理解有助于优化类的加载性能,减少不必要的初始化开销,并能够正确实现一些设计模式,确保类在多线程环境下的稳定性。

java">// 父类
class Parent {// 静态变量public static int parentStaticVar = initializeParentStaticVar();// 静态代码块static {System.out.println("Parent static block 1 executed");}// 静态代码块static {System.out.println("Parent static block 2 executed");}// 静态方法public static void parentStaticMethod() {System.out.println("Parent static method called");}// 实例变量public int parentInstanceVar;// 构造方法public Parent() {System.out.println("Parent constructor executed");this.parentInstanceVar = 1;}// 静态变量初始化方法private static int initializeParentStaticVar() {System.out.println("Initializing parentStaticVar");return 100;}
}// 子类
class Child extends Parent {// 静态变量public static int childStaticVar = initializeChildStaticVar();// 静态代码块static {System.out.println("Child static block executed");}// 静态方法public static void childStaticMethod() {System.out.println("Child static method called");}// 实例变量public int childInstanceVar;// 构造方法public Child() {super(); // 调用父类构造方法System.out.println("Child constructor executed");this.childInstanceVar = 2;}// 静态变量初始化方法private static int initializeChildStaticVar() {System.out.println("Initializing childStaticVar");return 200;}
}// 主方法
public class StaticExample {public static void main(String[] args) {System.out.println("Creating Child instance...");Child c = new Child(); // 创建子类实例// 调用静态方法Child.childStaticMethod();Parent.parentStaticMethod();}
}

执行顺序:

  1. 类加载:
  • 首先加载 Parent 类:

    • 静态变量 parentStaticVar 初始化,输出:
      Initializing parentStaticVar
      
    • 静态代码块按声明顺序执行,输出:
      Parent static block 1 executed
      Parent static block 2 executed
      
  • 然后加载 Child 类:

    • 静态变量 childStaticVar 初始化,输出:
      Initializing childStaticVar
      
    • 静态代码块执行,输出:
      Child static block executed
      
  1. 创建实例:
  • 创建 Child 类实例时,首先执行 Parent 类的构造方法,输出:
    Parent constructor executed
    
  • 接着执行 Child 类的构造方法,输出:
    Child constructor executed
    
  1. 调用静态方法:
  • 调用子类的静态方法 Child.childStaticMethod(),输出:
    Child static method called
    
  • 调用父类的静态方法 Parent.parentStaticMethod(),输出:
    Parent static method called
    

静态与线程安全

静态变量在类加载时初始化,并且在整个JVM中只有一份。所有线程访问的都是同一个静态变量,这代表不同线程对静态变量的操作可能会相互影响。如果静态变量在多个线程中被同时修改,可能会导致数据不一致或者其他线程安全问题。例如,如果两个线程同时修改一个静态计数器,没有同步机制的话,计数器的值可能会出现错误。

静态不安全解决方案:

  • 通过在静态方法或静态代码块中使用synchronized关键字,这样可以避免多个线程同时访问或修改静态变量。
    java">public class SynchronizedExample {private static int sharedCounter = 0;// 静态同步方法public static synchronized void incrementCounter() {sharedCounter++;}public static void main(String[] args) {Runnable task = () -> {for (int i = 0; i < 1000; i++) {incrementCounter();}};Thread t1 = new Thread(task);Thread t2 = new Thread(task);t1.start();t2.start();try {t1.join();t2.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Shared counter value: " + sharedCounter);}
    }
    
  • 有时候可以使用原子类,它们提供了线程安全的操作。原子类提供了无锁的线程安全操作,用于处理并发访问的场景。
    java">public class AtomicExample {private static final AtomicInteger atomicCounter = new AtomicInteger(0);public static void incrementAtomicCounter() {atomicCounter.incrementAndGet();}public static void main(String[] args) {Runnable task = () -> {for (int i = 0; i < 1000; i++) {incrementAtomicCounter();}};Thread t1 = new Thread(task);Thread t2 = new Thread(task);t1.start();t2.start();try {t1.join();t2.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Atomic counter value: " + atomicCounter.get());}
    }
    
  • 如果每个线程需要独立的静态变量副本,可以使用ThreadLocal类。ThreadLocal为每个线程提供一个独立的变量副本,避免了共享状态。
    java">public class ThreadLocalExample {private static final ThreadLocal<Integer> threadLocalCounter = ThreadLocal.withInitial(() -> 0);public static void incrementThreadLocalCounter() {threadLocalCounter.set(threadLocalCounter.get() + 1);}public static void main(String[] args) {Runnable task = () -> {for (int i = 0; i < 1000; i++) {incrementThreadLocalCounter();}System.out.println("Thread local counter value: " + threadLocalCounter.get());};Thread t1 = new Thread(task);Thread t2 = new Thread(task);t1.start();t2.start();try {t1.join();t2.join();} catch (InterruptedException e) {e.printStackTrace();}}
    }
    

http://www.ppmy.cn/embedded/93661.html

相关文章

路由导航守卫

路由导航守卫&#xff08;Navigation Guards&#xff09;是 Vue Router 提供的功能&#xff0c;用于控制用户在应用中的导航行为。简单来说&#xff0c;它们允许你在用户访问不同路由时执行一些代码&#xff0c;比如检查用户是否登录、加载数据或阻止导航等。 比喻&#xff1a…

电脑开机后出现bootmgr is missing原因及解决方法

最近有网友问我为什么我电脑开机后出现bootmgr is missing&#xff0c;这个提示意思是:意思是启动管理器丢失&#xff0c;说明bootmgr损坏或者丢失&#xff0c;系统无法读取到这个必要的启动信息导致无法启动。原因有很多&#xff0c;比如我们采用的是uefi引导&#xff0c;而第…

用户画像架构图

背景 本文讲述下实现一个画像平台的架构图 架构图 这里面的人群圈选我们这里主要采用ck和spark&#xff0c;不过也有很多使用es&#xff0c;如果使用es的话&#xff0c;需要把标签的数据也存储到es的表中&#xff0c;类似我们这里放到ck的表中一样&#xff0c;这样就可以通过…

【学习笔记】解决在声音输出中找不到蓝牙耳机设备的问题

【学习笔记】在声音输出中找不到蓝牙耳机设备 在使用蓝牙耳机的时候&#xff0c;遇见一个问题&#xff0c;就是在电脑在连接蓝牙耳机之后&#xff0c;在声音输出中找不到蓝牙耳机设备&#xff0c;只能使用扬声器播放声音。电脑使用的是Windows 11系统。后来在网上寻找解决方案…

自动驾驶系列—图像到IPM:深入解析IMP投影变换技术

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

【实现100个unity特效之19】使用ShaderGraph实现Unity 2D水

最终效果 文章目录 最终效果前言开始新增无光照影响的shaderGraph半透明效果&#xff0c;并且有一些颜色的变化其他办法更加复杂的2d水曲线河流不新建相机的方法实现2d水和物理交互效果&#xff08;Camera Sorting Layer Texture的使用&#xff09;参考完结 前言 先粗略记录一…

可视化基础的设计四大原则

一个好的数据可视化设计可以帮助观众迅速理解数据背后的意义。然而&#xff0c;如何确保我们的可视化设计既美观又简单易懂呢&#xff1f;本文将介绍四大设计原则——亲密原则、对比原则、对齐原则和重复原则。 1、 亲密原则&#xff08;Proximity&#xff09; 定义与应用&am…

Litestar GET function blocks OpenAI

题意&#xff1a;Litestar GET 函数阻塞 OpenAI 问题背景&#xff1a; When I transfer function to litestar, it suddenly stops OpenAI from returning a completion. I can print to console every declared variable except answer: 当我将函数传递给 litestar 时&#…