java的一个问题

renpan1003 2007-12-04 10:31:28
class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}

public class Mugs {
Mug c1;
Mug c2;
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
Mugs() {
System.out.println("Mugs()");
}
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
}
}
为什么输出顺序为
Inside main()
Mug(1)
Mug(2)
c1&c@ initialized
Mugs()
我自己想的与运行的不一样啊!
...全文
428 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
renpan1104 2008-03-05
  • 打赏
  • 举报
回复
evanka 2007-12-08
  • 打赏
  • 举报
回复
 {   
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}


我也想知道这个是什么,以前怎么没碰到过,没名字,也没关键字,哪位能讲一下吗?
IOPsyche 2007-12-06
  • 打赏
  • 举报
回复
楼主,感情我的代码你都没有运行一下?心里哇哇的凉啊!
2楼说的也不是完全正确,最起码那是个动态的代码块,不是静态的!
qing2005 2007-12-06
  • 打赏
  • 举报
回复
执行顺序:
1.类的静态代码块
2.main函数
3.非静态代码块
4.构造


public class Person {
{
System.out.println("This is code block1.");
}
static{
System.out.println("This is static code block1");
}
public Person() {
System.out.println("This is struct method.");
}
public static void main(String[] args){
System.out.println("This is main method.");
Person p1=new Person();
Person p2=new Person();
}
}


输出:
This is static code block1
This is main method.
This is code block1.
This is struct method.
This is code block1.
This is struct method.
niangao001 2007-12-06
  • 打赏
  • 举报
回复
学习了~~
ftgreat 2007-12-06
  • 打赏
  • 举报
回复
mark~
yuxingleo 2007-12-06
  • 打赏
  • 举报
回复
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
请问这个没有什么名字只有括号的函数是什么啊?
谁能给我详细讲解一下。
jason1985 2007-12-05
  • 打赏
  • 举报
回复
程序先从main()函数开始执行,然后按照自顶向下的程序输出规则,故会得到如此输出顺序
IOPsyche 2007-12-05
  • 打赏
  • 举报
回复
收回六楼的发言.抱歉.
IOPsyche 2007-12-05
  • 打赏
  • 举报
回复
大家研究一下类的初始化顺序,修改后的代码.

class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}

class Mugs {
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
}
Mugs() {
System.out.println("Mugs()");
}
{
c1 = new Mug(3);
c2 = new Mug(4);
c3 = new Mug(5);
System.out.println("c1 & c2 initialized");
}
Mug c1 = new Mug(1);
Mug c2 = new Mug(2);
Mug c3 = new Mug(6);
static{System.out.println("Outside main()");}
static Mug c0=new Mug(0);
}

执行结果:

Outside main()
Mug(0)
Inside main()
Mug(3)
Mug(4)
Mug(5)
c1 & c2 initialized
Mug(1)
Mug(2)
Mug(6)
Mugs()



从输出的4,5,6行来看,语句 Mug c1 = new Mug(1);被分成两部分来执行了.
IOPsyche 2007-12-05
  • 打赏
  • 举报
回复
不好意思,之前我的回答有点问题.

1、类只有在使用New调用创建的时候才会被JAVA类装载器装入,不要认为在这句之前内存中已经有了Mugs类了.

启动类(含main方法的类),在命令行中执行命令 :java Mugs 时就加载了.这句话只针对非启动类而言.
Deli_Station 2007-12-05
  • 打赏
  • 举报
回复
程序的执行顺序:程序块,父类函数,子类函数
renpan1003 2007-12-05
  • 打赏
  • 举报
回复
2楼的谢了.
学了C++再学JAVA容易混淆.
不过还是蛮轻松的.
Y17083 2007-12-05
  • 打赏
  • 举报
回复
请问
class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}

public class Mugs {
Mug c1;
Mug c2;
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}

Mugs() {
System.out.println("Mugs()");
}
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
}
}

class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}

class Mugs {
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
}
Mugs() {
System.out.println("Mugs()");
}
{
c1 = new Mug(3);
c2 = new Mug(4);
c3 = new Mug(5);
System.out.println("c1 & c2 initialized");
}

Mug c1 = new Mug(1);
Mug c2 = new Mug(2);
Mug c3 = new Mug(6);
static{System.out.println("Outside main()");}
static Mug c0=new Mug(0);
}
中的红色部分和匿名的inner class的区别是什么? 谢谢指教
xiaozhilover 2007-12-05
  • 打赏
  • 举报
回复
2楼正解!
zhangbaokun 2007-12-05
  • 打赏
  • 举报
回复
这个你得看书,不是靠想出来的
zzkk_1980 2007-12-05
  • 打赏
  • 举报
回复
首先static块-》main函数 -》实列块-》构造函数
HI_木林森 2007-12-05
  • 打赏
  • 举报
回复
输出完全正确啊.
醉面韦陀 2007-12-05
  • 打赏
  • 举报
回复
我觉得,jvm在加载类时候,首先执行main函数,相信大家都知道这个
然后调用静态初始化块,如果你new 了一个对象,调用对应类的构造方法,默认是无参的
所以,就是你这样的结果了!
zyclc 2007-12-05
  • 打赏
  • 举报
回复
有什么不对的?你原来认为是怎么个顺序?
加载更多回复(11)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧