首页 > 编程语言 > 详细

【java】在主函数前输出hello world

时间:2015-03-20 01:35:03      阅读:378      评论:0      收藏:0      [点我收藏+]

  在java语言中,main()方法是整个程序的入口,程序在运行时最先加载的就是main()方法,但是这并不意味着main()方法就是程序运行时第一个被执行的模块。

  在java语言中,静态代码块在类被加载时就会被调用,因此可以在main()方法前就执行,利用静态代码块实现在主函数之前输出hello world

public class staticc {
    static
    {
        System.out.println("hello world");
    }
    public static void main(String[] arg)
    {
        System.out.println("say hello to the world");
    }

}

输出:

hello world
say hello to the world

执行顺序与静态代码块的位置无关

public class staticc {
    /*static
    {
        System.out.println("hello world");
    } */
    public static void main(String[] arg)
    {
        System.out.println("say hello to the world");
    }
    static
    {
        System.out.println("hello world");
    }

}

输出:

hello world
say hello to the world

【java】在主函数前输出hello world

原文:http://caoyue.blog.51cto.com/9876038/1622360

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!