问几个java问题
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 12 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:bigfox (等级:4 - 马马虎虎,发帖:2214) 发表:2004-06-29 17:04:28  楼主  关注此帖
问几个java问题
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!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2004-06-29 18:03:25  2楼
Answers:
1) Of course no error, since you are accessing t.myString inside t itself! Note that your main method is part of the MyThread class. Furthermore, Java's default access specification for member variables and methods is NOT private! The default specification allows access from any class in the same package. As your MyThread class is not in any package (effectively in the so-called "global" package), and I suppose so is your other test classes, myString is accessible.

2) The x in the first static scope cannot be accessed, because it is destroyed straightaway after it is declared! Note that static {...} structure is usually useful only in the global scope, i.e., outside any class. Basically, it provides static initialization routines to be executed before everything else (of course, there are still necessary initializations like class loading before this). Don't confuse the static code fragment with static member variable of a class.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:香陵居士 (等级:16 - 好恐怖呀,发帖:22662) 发表:2004-06-29 21:46:34  3楼
java tutorial from Sun is always a good resource to consult
First q: t is an local variable, t.MyString is accessing a member variable of t via t. Thus it is nothing related to public/protected/private limitation.


Second q: static initialization blocks are used for initilize a class member.

Note declaration also do the initialization for class member/instance member:
byte,short,int,long -> 0
float, double, -> 0.0
bool -> false
char -> empty character
object -> null


If you really want to access the thing in static{}, put static statements before define class member.

Seems you are doing SCJP, if yest, better read Java tutorial carefully.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2004-06-30 11:30:45  4楼
java tutorial from Sun is always a good resource to consultFirst q: t is an local variable, t.MyString is accessing a member variable of t via t. Thus it is nothing related to public/protected/private limitation. Second q: static initialization blocks are used for initilize a class member. Note declaration also do the initialization for class member/instance member: byte,short,int,long -> 0 float, double, -> 0.0 bool -> false char -> empty character object -> null If you really want to access the thing in static{}, put static statements before define class member. Seems you are doing SCJP, if yest, better read Java tutorial carefully.
What's SCJP?
Sun Certified Java Programmer?

Is it an interesting cert?
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:小蹦 (等级:10 - 炉火纯青,发帖:3962) 发表:2004-06-30 13:00:58  5楼
ur mails...got a lot...how?
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:bigfox (等级:4 - 马马虎虎,发帖:2214) 发表:2004-06-30 15:36:01  6楼
ur mails...got a lot...how?
really? help me pass them to 阿修罗 bah 3x 3x
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:bigfox (等级:4 - 马马虎虎,发帖:2214) 发表:2004-06-30 16:01:17  7楼
Answers:1) Of course no error, since you are accessing t.myString inside t itself! Note that your main method is part of the MyThread class. Furthermore, Java's default access specification for member variables and methods is NOT private! The default specification allows access from any class in the same package. As your MyThread class is not in any package (effectively in the so-called "global" package), and I suppose so is your other test classes, myString is accessible. 2) The x in the first static scope cannot be accessed, because it is destroyed straightaway after it is declared! Note that static {...} structure is usually useful only in the global scope, i.e., outside any class. Basically, it provides static initialization routines to be executed before everything else (of course, there are still necessary initializations like class loading before this). Don't confuse the static code fragment with static member variable of a class.
are u saying that
any var(s) declared within static{} will not be reachable any more after the control flow gets out of that block?

pls correct me if i am wrong. 3x :D
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:bigfox (等级:4 - 马马虎虎,发帖:2214) 发表:2004-06-30 16:01:40  8楼
What's SCJP?Sun Certified Java Programmer? Is it an interesting cert?
not suit for u at all.
it is only for junior programmers, but it is the prerequisite cert for SCJD SCBCD etc.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:bigfox (等级:4 - 马马虎虎,发帖:2214) 发表:2004-06-30 16:56:01  9楼
really? help me pass them to 阿修罗 bah 3x 3x
只要不是来自PR/UOB, 就不是很急.
如果他没空,那我就找个时间过去一趟 huhu :)

你加我msn吧, smallfox33@
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2004-06-30 22:10:07  10楼
not suit for u at all.it is only for junior programmers, but it is the prerequisite cert for SCJD SCBCD etc.
That's it lo... there are always long staircases to climb...
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:香陵居士 (等级:16 - 好恐怖呀,发帖:22662) 发表:2004-07-01 00:39:08  11楼
That's it lo... there are always long staircases to climb...
Hoho, if you are good enough to get SCEA, can got for it directly. ;)
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2004-07-01 08:22:33  12楼
are u saying thatany var(s) declared within static{} will not be reachable any more after the control flow gets out of that block? pls correct me if i am wrong. 3x :D
Yes.
Static initializer blocks behave like static methods in the class, with all method names, argument lists, and return types stripped.

All static initializers declared in a class are executed exactly once, in the order of their declarations, when the class is first loaded. Therefore, static initializer are popular when you need to initialize static class member variables with logic that exceeds what a single-line expression initializer can do.

Therefore, with the basic scoping rule, I suppose local variables should not be accessible from outside of a static initializer block.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 12 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码