小白眼中的static关键字用法:

    1、static方法

          直接类名.方法命。如图:

          关键字之static

          关键字之static


    2、static变量

        静态变量是共享,在类初次加载时会被初始化。比如我要在多个地方用到一个叫String类型为“CSDN”的数据。

只需用static定义一次就好。

        private static final String CSDN= "CSDN";

这里定义的CSDN就代表“CSDN”。

    3、static代码块

        只会在程序加载时执行一次。可以节省大量的资源,内存。例如:

      static  {
        System.out.println("静态代码块");   
        } 

上面3种是小白眼中的static的用法。


补充:

    4、静态内部类。

用静态内部类实现单例模式:

public class Singleton {
    private static class LazyHolder {
        private static final Singleton INSTANCE = new Singleton();
    }
    private Singleton (){}
    public static Singleton getInstance() {
        return LazyHolder.INSTANCE;
    }

}

    5、static导包

   直接上图,本人小白感觉不到这种写法优势,嘻嘻--- 如图:

关键字之static


        

相关文章:

  • 2021-12-07
  • 2021-12-08
  • 2021-08-13
  • 2021-05-01
  • 2021-11-21
猜你喜欢
  • 2021-07-23
  • 2022-02-23
  • 2022-02-15
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案