zoukankan      html  css  js  c++  java
  • static修饰的代码块被称作静态代码块

    static修饰代码块:

        在java中,被static修饰的代码块被称作静态代码块。静态代码块在类被加载时,就会被执行,并且只会执行一次(类只会加载一次)。

    详见实例:

    定义一个学生类:

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

    public void showName (String name){
    System.out.println(name);
    }
    }
    定义一个测试类:

    public class Test2 {
    public static void main(String[] args) {
    Student2 s1=new Student2();
    Student2 s2=new Student2();
    s1.showName("张三");
    s2.showName("李四");
    }
    }
    输出结果:

    根据输出结果,静态代码块仅被执行一次,并且在类加载时就开始执行,优先于类中的方法。  
    ————————————————
    版权声明:本文为CSDN博主「LIAO_7053」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/LIAO_7053/java/article/details/81408139

  • 相关阅读:
    Run Shell Commands in Python
    Install Fabric 1.8.3 Manually on Ubuntu 12.04
    Setup a Simple HTTP Proxy Server
    去掉文件中的^M
    Build Web Server with Apache and Passenger
    Delete Trailing Spaces with Vim
    Specify Default JDK on Ubuntu
    总结
    问题
    HTTPS 和 HTTP
  • 原文地址:https://www.cnblogs.com/cxxiao/p/12672898.html
Copyright © 2011-2022 走看看