问几个java问题
所在版块:求学狮城 发贴时间:2004-06-29 17:04

用户信息
复制本帖HTML代码
高亮: 今天贴 X 昨天贴 X 前天贴 X 
What will be the output on compiling/running the following code?

public class MyThread implements Runnable
{
String myString = "Yes ";

public void run()
{
this.myString = "No ";
}

public static void main(String[] args)
{
MyThread t = new MyThread();
new Thread(t).start();

for (int i=0; i < 10; i++)
System.out.print(t.myString);
}
}

问题是,myString 不是public的,为什么main()最后一行不会报错呢? 我把main()移到另一个class中,也能run,都有点学糊涂了我 :P


第二个问题

public class Static
{
static
{
int x = 5;
}

static int x,y;
public static void main(String args[])
{
x--;
myMethod();
System.out.println(x + y + ++x);
}

public static void myMethod()
{
y = x++ + ++x;
}
}

这里有两个static, 一个是static{ int x = 5;} 另一个是static int x;
如何拿到在那个static{}中的x? 还想知道前者有什么用吗?


多谢!!
.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!

Put your OWN COOL signature here!
 相关帖子 我要回复↙ ↗回到正文
问几个java问题 bigfox   (946 bytes , 622reads )
ur mails...got a lot...how? 小蹦   (0 bytes , 229reads )
really? help me pass them to 阿修罗 bah 3x 3x bigfox   (0 bytes , 369reads )
只要不是来自PR/UOB, 就不是很急. bigfox   (69 bytes , 303reads )
java tutorial from Sun is always a good resource to consult 香陵居士   (591 bytes , 281reads )
What's SCJP? Flying   (58 bytes , 289reads )
not suit for u at all. bigfox   (86 bytes , 369reads )
That's it lo... there are always long staircases to climb... Flying   (0 bytes , 277reads )
Hoho, if you are good enough to get SCEA, can got for it directly. ;) 香陵居士   (0 bytes , 229reads )
Answers: Flying   (944 bytes , 294reads )
are u saying that bigfox   (150 bytes , 401reads )
Yes. Flying   (582 bytes , 268reads )