由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 问一个java的面试题 (转载)
相关主题
help on this scope questionget set
Why inner classes can access only local final variables?java inner class - 初学者问
inner class 让认糊涂JAVA 求解
请教:怎么把synchronize的method改成用thread 但thread safe呢?access java parent class protected variable question
Java大侠们:Hashtable help please!JavaBean variable name standard
[合集] java 得 inner class 有没有 static 有什么区别啊?how to solve the problem: the members change by each other .
array 在java里 是一定放在heap 吗?It's a Java Bug
anyone saw this on code?Answer to "Is this a Bug or not? "
相关话题的讨论汇总
话题: class话题: local话题: variable话题: inner话题: method
进入Java版参与讨论
1 (共1页)
i******e
发帖数: 1277
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: itistrue (MJ), 信区: JobHunting
标 题: 问一个java的面试题
发信站: BBS 未名空间站 (Mon Apr 18 15:02:03 2011, 美东)
G家面试时被问到的,当时没答出来。
keyword "final" 在什么时候是required?也就是说,如果不加这个"final",编译不
会通过?
g*****g
发帖数: 34805
2
When the parameter is referenced in anonymous inner class.

【在 i******e 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: itistrue (MJ), 信区: JobHunting
: 标 题: 问一个java的面试题
: 发信站: BBS 未名空间站 (Mon Apr 18 15:02:03 2011, 美东)
: G家面试时被问到的,当时没答出来。
: keyword "final" 在什么时候是required?也就是说,如果不加这个"final",编译不
: 会通过?

m****r
发帖数: 6639
3
ft. how do you know these things?

【在 g*****g 的大作中提到】
: When the parameter is referenced in anonymous inner class.
l******e
发帖数: 12192
4
用过都知道呀
连我都知道

【在 m****r 的大作中提到】
: ft. how do you know these things?
m****r
发帖数: 6639
5
ok. i need to use it more.

【在 l******e 的大作中提到】
: 用过都知道呀
: 连我都知道

i****k
发帖数: 804
6
or when the formal parameter is declared to be final.

【在 g*****g 的大作中提到】
: When the parameter is referenced in anonymous inner class.
q*********u
发帖数: 280
7
gwt里面好多这种用法

ft. how do you know these things?

【在 m****r 的大作中提到】
: ft. how do you know these things?
g*****g
发帖数: 34805
8
手写线程的时候其实用得很多。

【在 m****r 的大作中提到】
: ft. how do you know these things?
l**********n
发帖数: 8443
9
simple question.....

【在 i******e 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: itistrue (MJ), 信区: JobHunting
: 标 题: 问一个java的面试题
: 发信站: BBS 未名空间站 (Mon Apr 18 15:02:03 2011, 美东)
: G家面试时被问到的,当时没答出来。
: keyword "final" 在什么时候是required?也就是说,如果不加这个"final",编译不
: 会通过?

l**********n
发帖数: 8443
10
这也太初级了吧。

【在 i******e 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: itistrue (MJ), 信区: JobHunting
: 标 题: 问一个java的面试题
: 发信站: BBS 未名空间站 (Mon Apr 18 15:02:03 2011, 美东)
: G家面试时被问到的,当时没答出来。
: keyword "final" 在什么时候是required?也就是说,如果不加这个"final",编译不
: 会通过?

相关主题
[合集] java 得 inner class 有没有 static 有什么区别啊?get set
array 在java里 是一定放在heap 吗?java inner class - 初学者问
anyone saw this on code?JAVA 求解
进入Java版参与讨论
x*****p
发帖数: 1707
11
Very junior question
x***i
发帖数: 585
12
答案是这个,但是原因有没有人知道?
我看了thinking in java。里面好像提到:
inner class may survive longer than the outer class local variable. So it
must be final. then we can treat it as const.
好像还是不够明白,不知道大家怎么理解?
g*****g
发帖数: 34805
13
Let's say you start a thread running an anonymous inner class from a method.
You pass a local variable into the anonymous inner class. If the variable
is final, the method stack can be removed when the method finishes.If not,
then it has to wait until the inner class finishes, which can cause a memory
leak.

【在 x***i 的大作中提到】
: 答案是这个,但是原因有没有人知道?
: 我看了thinking in java。里面好像提到:
: inner class may survive longer than the outer class local variable. So it
: must be final. then we can treat it as const.
: 好像还是不够明白,不知道大家怎么理解?

r*****l
发帖数: 2859
14
To add two more points:
1, When a parameter of the method or a LOCAL VARIABLE defined in the method
initiating the anonymous inner class, final keyword is needed.
2, This rule also applies to local inner class (inner class defined in a
method).

【在 g*****g 的大作中提到】
: When the parameter is referenced in anonymous inner class.
r*****l
发帖数: 2859
15
Non-final variable is put in stack. When the method finished, the stack
popped and the variable is gone. When the inner class method (can live
longer) is trying to access the variable, it cannot find a value.
Final/const variable is put in heap. It will be there after the method
finished.

【在 x***i 的大作中提到】
: 答案是这个,但是原因有没有人知道?
: 我看了thinking in java。里面好像提到:
: inner class may survive longer than the outer class local variable. So it
: must be final. then we can treat it as const.
: 好像还是不够明白,不知道大家怎么理解?

q*****9
发帖数: 85
16
Actually, it's not because it cannot find a value, java mechanism
will generate a constructor for inner class, and copy the local
variable to this constructor, obviously the inner class could keep
the local variable at this time rather than bothering with outer
variable, then some one might change the value such that the variable
is of different values which may be undesirable. To prevent this from
happening, just mark the variable as final to let the inner class
know it cannot be changed.

【在 r*****l 的大作中提到】
: Non-final variable is put in stack. When the method finished, the stack
: popped and the variable is gone. When the inner class method (can live
: longer) is trying to access the variable, it cannot find a value.
: Final/const variable is put in heap. It will be there after the method
: finished.

s******e
发帖数: 493
17
Are you sure that a method local class will create a instance variable for
each local variable it refers to and populate the value in a generated
construtor?
Can you post the link to the article you read about this. Or kindly answer
the following questions for me.
1. when are the instance variable definitions created? and how?
2. what if my method local class already have a constructor?
r*****l
发帖数: 2859
18
I have to debate with you otherwise people reading this post will get
confused. I would be ok if I were proven wrong but I need some reasoning.
"obviously the inner class could keep the local variable at this time rather
than bothering with outer variable"
Where does the inner class keep the local variable? And if it keeps a local copy, why does the inner class care if the original copy is changed?
Think about method call, when you are in the method, the method copies the parameters in its stack and it has its local copies. And the method does not care if the original copies are changed or not since it just works on its local copy.

【在 q*****9 的大作中提到】
: Actually, it's not because it cannot find a value, java mechanism
: will generate a constructor for inner class, and copy the local
: variable to this constructor, obviously the inner class could keep
: the local variable at this time rather than bothering with outer
: variable, then some one might change the value such that the variable
: is of different values which may be undesirable. To prevent this from
: happening, just mark the variable as final to let the inner class
: know it cannot be changed.

g*****g
发帖数: 34805
19
Obviously inner class can copy the variable if Java chooses to
implement it that way. But data inconsistency is something that
leads to bug. A final requirement is clean, and most of the time
not a restriction.

rather
local copy, why does the inner class care if the original copy is changed?
parameters in its stack and it has its local copies. And the method does
not care if the original copies are changed or not since it just works on
its local copy.

【在 r*****l 的大作中提到】
: I have to debate with you otherwise people reading this post will get
: confused. I would be ok if I were proven wrong but I need some reasoning.
: "obviously the inner class could keep the local variable at this time rather
: than bothering with outer variable"
: Where does the inner class keep the local variable? And if it keeps a local copy, why does the inner class care if the original copy is changed?
: Think about method call, when you are in the method, the method copies the parameters in its stack and it has its local copies. And the method does not care if the original copies are changed or not since it just works on its local copy.

s******e
发帖数: 493
20
Actually in java, method local class once compiled will be no different fom
other classes (the semantic scope might be different). To make this happen,
method local class has to keep copy of local variables it refers. But how
compiler does it, I have never seen any books and articles talking about It.
Somebody posted above suggested that compiler uses constructor to do this.
I am not sure if it is correct.
The final variables usually are saved in some run time area(different from
stack and heap). The guy posted above is correct that the root reason of
method local class can only access final variables is not because the local
variable lives in stack and have shorter life. That is more like derivative
impact. The root reason is the confusion in synching the two copies of
variables. Actually when first tried to implemented it, there is no final
keywords limitation, but since java really doesn't support closure, lots of
confusions were raised. So the limitation was added. But according to the
designer, the closure support might be added in the future and the
limitation might be dropped then.
相关主题
access java parent class protected variable questionIt's a Java Bug
JavaBean variable name standardAnswer to "Is this a Bug or not? "
how to solve the problem: the members change by each other .入门Java CLASSPATH问题:
进入Java版参与讨论
r*****l
发帖数: 2859
21
If the variable is not defined in a class, then JVM specs do not demand a
local copy. Some JVM implementation may decide to create a local copy but
they need to handle the sync and make it transparent to the programmer. It
cannot say that: programmer, you need to mark parameters and local variable
as "final" because I have problem syncing the copies. And not to make local
copies is a neater design.
Again, to declare parameters and local variable as "final" is to make it
living longer then the method (if necessary) so that the inner class can
still use it even after the method call is finished.

fom
,
It.
.
local
derivative

【在 s******e 的大作中提到】
: Actually in java, method local class once compiled will be no different fom
: other classes (the semantic scope might be different). To make this happen,
: method local class has to keep copy of local variables it refers. But how
: compiler does it, I have never seen any books and articles talking about It.
: Somebody posted above suggested that compiler uses constructor to do this.
: I am not sure if it is correct.
: The final variables usually are saved in some run time area(different from
: stack and heap). The guy posted above is correct that the root reason of
: method local class can only access final variables is not because the local
: variable lives in stack and have shorter life. That is more like derivative

s******e
发帖数: 493
22
All classes are "equal" once compiled no matter where you define them (the
semantic scope might be different depending on where you define your classes
). I think java classes are designed following this principle.
without a local copy, how can you make it happen?
r*****l
发帖数: 2859
23
Look at this example:
Class A {
public static int i = 5;
}
Class B{
public void print() {
System.out.println(A.i);
}
}
Do you think B will hold a local copy of A.i? I don't think so. Ok, you
might argue that there does exist a local copy since we don't know JVM's
implementation. Even if a local copy exists, JVM has to make sure that
whenever some code modifies A.i, B has to make sure that the local copy is
in sync. That's equivalent to just no local copy.

classes

【在 s******e 的大作中提到】
: All classes are "equal" once compiled no matter where you define them (the
: semantic scope might be different depending on where you define your classes
: ). I think java classes are designed following this principle.
: without a local copy, how can you make it happen?

s******e
发帖数: 493
24
I think you totally misunderstand the nature of this issue.
In your example, since i is public and static, the publication of i should
not be an issue. Why does B need to maintain a copy of sth is public to
anybody (at least in the same scope like package)? I do not know exactly how
jvm implementers realize it. different implementers may choose to implement
it in a different way. But I think that common knowledge is that i is in
the same run time area as final variable. But no matter what, for a public
thing like that, no other classes will never need to create a local copy for
itself.
The reason that the compiler needs to create a local copy for a local class
or a pacakge level method to access a enclosing class private variables is
because directly accessing sth private or with a limited scope to you is
against at least encapsulation.
let me give you a better example to help you understand
public class A
{
private int i;
Class B
{
public int getI()
{
return i;
}
}
}
in this case, since once compiled, class A and B will be independent (not
mean that they are not coupled) classes, if we allow B to access A's private
variable, it is against encapsulation. What the compiler does is to create
a package level method to expose i from A, so B can call it.
B doesn't need to maintain a local copy for i either.
In method local class, due to sematic scop limitation, there is no such
thing can be done.
r*****l
发帖数: 2859
25
First, you understanding of whether/how inner class accesses enclosing class
's private member is not correct. In your example, B is an inner class of A
and you said "class A and B will be independent". This is not right. In fact
B is a member (class) or A. You also mentioned "if we allow B to access A's
private variable, it is against encapsulation". This is again not correct.
Since B is A's member, it can access A's other private member.
Direct quote from oracle.com regarding inner class "A nested class is a
member of its enclosing class. Non-static nested classes (inner classes)
have access to other members of the enclosing class, even if they are
declared private"
Second, I understand the issue well. The example I provided is to show that
as long as a class can access a variable not defined in itself, it does not
have to create "local copy". This should apply to inner class as well.
The nature that normal inner class accessing enclosing class's members is
the same to method local inner class accessing method parameters and method
local variables. The only difference is that the parameters and method local
variables are put in stack if not declared as "final". As a comparison,
enclosing class does not have to declare its members as "final" for the
inner class to access.
You mentioned that the reason is "The root reason is the confusion in
synching the two copies of variables". That's the same as "data
inconsistency" from goodbug. I can tell you that the Java does not maintain,
and Java does not want to maintain, data consistency in this scenario (
inner class using an outside variable)
I suggest that you design a test like this:
1, Declare a variable in outer class (not final).
2, Define an inner class that print the above declared variable twice, with
a 2-second delay between the prints.
3, In the outer class, change the value of the above declared variable
between the two prints.
The result will tell you everything.

how
implement
for
class

【在 s******e 的大作中提到】
: I think you totally misunderstand the nature of this issue.
: In your example, since i is public and static, the publication of i should
: not be an issue. Why does B need to maintain a copy of sth is public to
: anybody (at least in the same scope like package)? I do not know exactly how
: jvm implementers realize it. different implementers may choose to implement
: it in a different way. But I think that common knowledge is that i is in
: the same run time area as final variable. But no matter what, for a public
: thing like that, no other classes will never need to create a local copy for
: itself.
: The reason that the compiler needs to create a local copy for a local class

s******e
发帖数: 493
26
Do me a favor. Just compile the example class I posted and check the classes
generated by compiler, and you will understand what I said.
r*****l
发帖数: 2859
27
You mentioned a lot and which one you want me to understand?
From your posts, I listed two sentences that are most closely related to why
we need "final" for method parameters and local variables access by method
local or anonymous class.
1, "To make this happen, method local class has to keep copy of local
variables it refers".
2, "The root reason is the confusion in synching the two copies of variables"
I don't see why your example can be supportive.

classes

【在 s******e 的大作中提到】
: Do me a favor. Just compile the example class I posted and check the classes
: generated by compiler, and you will understand what I said.

s******e
发帖数: 493
28
you have to read my poster carefully and in full. As I said, once the
classes are compiled, they are independent. The different type of inner
nclasses might still couple with the enclosing class, but they must follow
the same rule like that they are different classes. For source code level,
there is so called member class, but not after compiled.
Also as I said, the reason that it was designed in this way is due to the
fact that java do not support function closure as many script languages do.
Still do not get it? well good luck!
r*****l
发帖数: 2859
29
I kept asking for your explanation on the following statement in your post.
"To make this happen, method local class has to keep copy of local
variables it refers"
The above statement is wrong. The reason I responded your post is to make is clear so that other people will not be confused.
I did do you a favor to compile your example. The results just showed the
opposite: after compilation, the inner class does not keep a copy. It just
hold a reference to access the outer class variable. If the outer class
changed that variable, the inner class will see the change when it tries to
access it. There is no such thing called "copy of local variable". Since
there is only one variable, what is the basis of what you said: "The root
reason is the confusion in synching the two copies of variables".
Frankly, I consider that we are in a technical discussion. The point is not
to show that "who" is right/wrong but "what" is right/wrong.
Anyway I don't see that you directly respond to the above points in your
recent posts and I am hoping we make is a closure on the "local variable"
issue.

【在 s******e 的大作中提到】
: you have to read my poster carefully and in full. As I said, once the
: classes are compiled, they are independent. The different type of inner
: nclasses might still couple with the enclosing class, but they must follow
: the same rule like that they are different classes. For source code level,
: there is so called member class, but not after compiled.
: Also as I said, the reason that it was designed in this way is due to the
: fact that java do not support function closure as many script languages do.
: Still do not get it? well good luck!

r*****l
发帖数: 2859
30
About whether an inner class can access outer class private variable. I
posted the direct quote from Oracle website and I am posting it again: "An
instance of an inner class can exist only within an instance of its
enclosing class and has access to its enclosing class's members even if they
are declared private." At the language level, this is the Java specs and
design.
JVMs are free to implement the above specs in any form. You wanted to use
your example to show how Sun JDK implements it to support the above specs. I
see your point. Because Sun JDK's implementation sticks to the specs, I don
't see discrepancy here.
If you read my post carefully and in full, you should see that I am focusing
on the statements of "copy of local variables". Maybe you wanted to use your example
to proof it but it proofs the opposite.
The test I suggested in my July 13 post can show that there is effectively
no "copy of local variable" without looking into compiled class. That test
will yield the same result for every JVM implementation.

【在 s******e 的大作中提到】
: you have to read my poster carefully and in full. As I said, once the
: classes are compiled, they are independent. The different type of inner
: nclasses might still couple with the enclosing class, but they must follow
: the same rule like that they are different classes. For source code level,
: there is so called member class, but not after compiled.
: Also as I said, the reason that it was designed in this way is due to the
: fact that java do not support function closure as many script languages do.
: Still do not get it? well good luck!

相关主题
Re: 怎样不用main(String args[])输出"hello worlWhy inner classes can access only local final variables?
替朋友请教一个问题inner class 让认糊涂
help on this scope question请教:怎么把synchronize的method改成用thread 但thread safe呢?
进入Java版参与讨论
s******e
发帖数: 493
31
First, you should understand that method local class is different from
regular inner class. If not, please google it, and you will know the
difference.
i asked you to compile the example class I posted because you had a trouble
to understand that what I said, "once compiled, the innner and enclosing
classes are independent (of course they are coupled)".
Did you try to create and compile a method local inner class trying to use a
final local variable? if not, please do it. The mechanism will be different
since in this case, the semantic scope that method local class tries to
access is different for local variable. Of course for enclosing class
variables, the same meachinsm should be applied.
r*****l
发帖数: 2859
32
Man, if you read my post carefully, you should know that I am well aware of the difference. Did you actually read my post before you respond? The difference between them is their scopes, but not the way they behave. If regular inner class does not create a copy of variable that does not belong to it. The method local inner class does not create a copy of variable that does not belong to it either. Keep in mind here that a final local variable is just a constant.
I did not question "once compiled, the inner and enclosing class are
independent". This is not even remotely related to LZ's question. I am questioning your statement about inner class needs to create "copy of local variable".
As I mentioned, I DID compile your example about regular inner class but it just showed no copy of variable.

trouble
a
different

【在 s******e 的大作中提到】
: First, you should understand that method local class is different from
: regular inner class. If not, please google it, and you will know the
: difference.
: i asked you to compile the example class I posted because you had a trouble
: to understand that what I said, "once compiled, the innner and enclosing
: classes are independent (of course they are coupled)".
: Did you try to create and compile a method local inner class trying to use a
: final local variable? if not, please do it. The mechanism will be different
: since in this case, the semantic scope that method local class tries to
: access is different for local variable. Of course for enclosing class

s******e
发帖数: 493
33
Oh man. I am just wasting my time...
PLEASE read my post CAREFULLY. Did I ever say that inner class has to keep a
local reference to the enclosing class variables? Here is what I said:
let me give you a better example to help you understand
public class A
{
private int i;
Class B
{
public int getI()
{
return i;
}
}
}
in this case, since once compiled, class A and B will be independent (not
mean that they are not coupled) classes, if we allow B to access A's private
variable, it is against encapsulation. What the compiler does is to create
a package level method to expose i from A, so B can call it.
B doesn't need to maintain a local copy for i either.
I clearly said that B didn't need to maintain a local copy for i in this
case.
The local copy will be needed for a method local class (also refered as
local class by some people) to access a final local variable(this is a
common term for method local variable. For class varaible, we usually call
it instance varaible or class varible if at class level).
Anyway, it is not my intention to correct what you think is right. Just
trying to clear some confusion to others want to learn. But it is just too
much...
r*****l
发帖数: 2859
34
I did not question "inner class has to keep a local reference to the
enclosing class variables"
I am questioning "keep copy of local variables", which is also in your post.
Ok, to remind you: "method local class once compiled will be no different fom other classes (the semantic scope might be different)" The other class here also include regular inner class. And you mentioned that method local class needs to keep a "copy of local variable", a variable that does not belong to the inner class. Per the "no different" rule, regular inner class will hold a copy of variable it needs to access that does not belong to the inner class, which is not true.

a

【在 s******e 的大作中提到】
: Oh man. I am just wasting my time...
: PLEASE read my post CAREFULLY. Did I ever say that inner class has to keep a
: local reference to the enclosing class variables? Here is what I said:
: let me give you a better example to help you understand
: public class A
: {
: private int i;
: Class B
: {
: public int getI()

1 (共1页)
进入Java版参与讨论
相关主题
Answer to "Is this a Bug or not? "Java大侠们:Hashtable help please!
入门Java CLASSPATH问题:[合集] java 得 inner class 有没有 static 有什么区别啊?
Re: 怎样不用main(String args[])输出"hello worlarray 在java里 是一定放在heap 吗?
替朋友请教一个问题anyone saw this on code?
help on this scope questionget set
Why inner classes can access only local final variables?java inner class - 初学者问
inner class 让认糊涂JAVA 求解
请教:怎么把synchronize的method改成用thread 但thread safe呢?access java parent class protected variable question
相关话题的讨论汇总
话题: class话题: local话题: variable话题: inner话题: method