Topic: 运行没有main函数的Java类 |
Print this page |
1.运行没有main函数的Java类 | Copy to clipboard |
Posted by: linux_china Posted on: 2005-11-18 09:46 通常我们需要在java类中添加main函数才能在命令行运行这个类,但是有一种方面确可以让你在没有main函数的情况下也可以运行java class。 建立一个新的class,代码如下:
static语句块会在加载类的时候加载,它在main函数之前运行。 当static语句块运行完毕后,会寻找main函数,如果没有找到,就会i抛出异常。但是在以上的代码中,static语句块执行后,我们终止程序的运行,所以异常就不会被抛出啦。 |
2.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: bluecrystal Posted on: 2005-11-18 10:37 晕死 |
3.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-11-18 15:34 To linux_China Please don't take this code as a good example to run Java programs. It is a bad example and bad code practice. It is especially bad for teaching beginners! To beginners Please don't learn this kind of trick, please don't!!! Thanks all!!! |
4.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: langziyu Posted on: 2005-11-18 15:59 同感,C/C++如果没有主函数的话,不能运行。 这也许是与其他编程语言的区别吧!!! 但不管如何,他的环境变量是必须的吗 一只菜鸟 |
5.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: bukaoyan Posted on: 2005-11-18 16:15 呵呵 这方法不错!! |
6.Re:运行没有main函数的Java类 [Re: bukaoyan] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-11-18 16:28 bukaoyan wrote: No, 这方法 is very very bad!!!! If you write a real application use such trick, you will get fired by any decent company. Please don't learn this trick, please don't!!!!! Thanks! |
7.Re:运行没有main函数的Java类 [Re: gongshi] | Copy to clipboard |
Posted by: wmgreat Posted on: 2005-11-18 20:29 gongshi wrote: gongshi是一个非常严厉的版主。:) 说实话,明白静态变量的初始化过程并不是一个坏事,更深一步,可以深入到类装载。 我想,一个好学的beginner应该不会把这些代码作为猎奇的对象,而是应该研究其所以然。 |
8.Re:运行没有main函数的Java类 [Re: wmgreat] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-11-19 01:57 wmgreat wrote: Yes, you're correct! It is a hard topic for beginners. However, the title was not "静态变量的初始化过程". but " 运行没有main函数的Java类 " That is why it was very bad!!!! |
9.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: Infinite Posted on: 2005-11-20 02:15 呵呵,好巧妙阿。 |
10.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: Jcat Posted on: 2005-11-20 14:07 不得不佩服师兄的创意~ 更不得不佩服大猫的严谨,向你学习! |
11.Re:运行没有main函数的Java类 [Re: Jcat] | Copy to clipboard |
Posted by: linux_china Posted on: 2005-11-20 21:07 建议初学者不要这么用,这纯粹是技巧,没有任何实际意义.慎用. |
12.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: francis36 Posted on: 2005-11-20 22:38 我相信这是JAVA独特的一面~~~ |
13.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-11-21 00:59 linux_china wrote: Thanks, linux_china, my dear friend !!!! Actually, we can write a total C-like code by only using static. I used to work in a telecom company, their all C++ code was not OO at all, just C code compiled in the C++ compiler. I was the first programmer to write OO code there. I know also another telecom company, all their Java code are actually C, but with Java syntex. Haha, what you pointed out is actually a 后门 for Java programmers to write C-like code... Of course, we can use main to 走后门 too... We don't want to 开后门 or 走后门, do we??? |
14.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: 空心菜 Posted on: 2005-11-24 15:11 这个是think in java 里面的例子 仔细看过这本书的,肯定有印象的 作者让读者演示给别人看的,没想到楼主把他放在这里了 理当放在水区 既然是放在java基础版,我觉得不如改下标题 如“静态变量的初始化过程”或“类最早初始化谁?” |
15.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: wangyabing Posted on: 2005-12-29 20:28 真好!主初学java的人感到它的无穷呀! |
16.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: cjyzpcl Posted on: 2005-12-31 15:45 这个我一般是放在类里面初始化读配置文件的。 |
17.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: XiaoN Posted on: 2005-12-31 22:06 这是《core java》里的一个代码 |
18.Re:运行没有main函数的Java类 [Re: linux_china] | Copy to clipboard |
Posted by: solo Posted on: 2006-01-01 18:26 我想的是没有main函数的话,你要把什么编译成。EXE文件呢? |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |