码迷,mamicode.com
首页 > 编程语言 > 详细

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

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

标签:java语言   hello   public   程序   world   

  在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

标签:java语言   hello   public   程序   world   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!