avatar
Is this a Bug or not?# Java - 爪哇娇娃
g*y
1
SDK 2.0
The following code:
interface A
{
}
class B
{

public A play(){
class C implements A
{
private C()
{
}
}
return new C();
}
}
when you compile it, how may class file you will get???
3??? no, 4.
A.class, B.class, B$1.class, B$1$C.class. Among those files
B$1.class infact are useless.
Why this will happen?
This is simplely because you
avatar
m*t
2
Well as xt said one example would be when you want to give an event
handler which is only relevant to the current class. You have two
ways to do it nicely. One is to put it in the same class file but as
a separate class, while another one is to use inner class to
put it at where you register
your event handler. The first approach has two cons, compared with
the second one. First your event handler class will be visible to
the entire package. Second it offers less readability because a
code

【在 g****y 的大作中提到】
: SDK 2.0
: The following code:
: interface A
: {
: }
: class B
: {
:
: public A play(){
: class C implements A

avatar
m*a
3
To me the most frustrating thing about inner class is like:
//-----------------------------------------------------------
class A{
protected int a=10;
}
public class B{
class C extends A{
}
C c;
public B(){
c=new C();
}
public static void main(String args[]){
System.out.println(new B().c.a);
}
}
//-----------------------------------------------------------

【在 m******t 的大作中提到】
: Well as xt said one example would be when you want to give an event
: handler which is only relevant to the current class. You have two
: ways to do it nicely. One is to put it in the same class file but as
: a separate class, while another one is to use inner class to
: put it at where you register
: your event handler. The first approach has two cons, compared with
: the second one. First your event handler class will be visible to
: the entire package. Second it offers less readability because a
: code

avatar
xt
4

Well, there is a third way, and we usually use this:
implement the event handling interfaces in the same class, then
add this as the event listener.
If the event handling is not terribly complicated, this is a good
choice, which give nice integrity of code. The implication of this
method is possible memory leak. Therefore when the object is no
longer in use, makes sure the event handler is removed.
We have built our product with about one hundred windows in the
GUI with this, and it really give

【在 m******t 的大作中提到】
: Well as xt said one example would be when you want to give an event
: handler which is only relevant to the current class. You have two
: ways to do it nicely. One is to put it in the same class file but as
: a separate class, while another one is to use inner class to
: put it at where you register
: your event handler. The first approach has two cons, compared with
: the second one. First your event handler class will be visible to
: the entire package. Second it offers less readability because a
: code

avatar
m*t
5

It certainly does! Or technically it does. 8-)
the garbage collectors in most JVM's use memory marking
instead of reference counting so that they can handle circular
referencing correctly.
Most memory leak problems in Java come from the underlying JVM
itself because JVM's are usually written in C++.

【在 xt 的大作中提到】
:
: Well, there is a third way, and we usually use this:
: implement the event handling interfaces in the same class, then
: add this as the event listener.
: If the event handling is not terribly complicated, this is a good
: choice, which give nice integrity of code. The implication of this
: method is possible memory leak. Therefore when the object is no
: longer in use, makes sure the event handler is removed.
: We have built our product with about one hundred windows in the
: GUI with this, and it really give

avatar
xt
6

Maybe in 1.3. But in 1.2 it is not happening. That's why we actually
solved the memory leaks. The only memory leak, and it is ridiculous,
we did not solve is this:
When you open a window, set focus to a JTextField, then as the cursor
blinks, it takes momory. We have observed a 12-16K/blink memory leak.
Obviously Sun is aware of this, since there are quite a few bug
reports on this. They didn't want to fix because they don't think it
is a bug - it will be gc'd in the end.
I think it is one of th

【在 m******t 的大作中提到】
:
: It certainly does! Or technically it does. 8-)
: the garbage collectors in most JVM's use memory marking
: instead of reference counting so that they can handle circular
: referencing correctly.
: Most memory leak problems in Java come from the underlying JVM
: itself because JVM's are usually written in C++.

avatar
xt
7

this is not quite true. We do have different GUI objects having
different semantics. For example, the "Back" "Next" "Cancel" "Finish"
buttons on a wizard. We do it with the same class. The way to do is
to find out the source of the event, and handle it differently.
Of course, it takes a linear time complexity to do this. But it won't
take much time, considering
the number of objects a frame is usualy very small - there is no reason
for a reasonable GUI to get that many objects in a single windo

【在 m******t 的大作中提到】
:
: It certainly does! Or technically it does. 8-)
: the garbage collectors in most JVM's use memory marking
: instead of reference counting so that they can handle circular
: referencing correctly.
: Most memory leak problems in Java come from the underlying JVM
: itself because JVM's are usually written in C++.

avatar
m*t
8

I'm not worried by performance either. 8-) You usually learn to
forget what you learned from the algorithms course one year after
you get a real job. 8-)
Your approach certainly works, and works well. However, I wouldn't
use it in my design if ever possible. The reason is simple - you are
mixing multiple functionalities in one function, and using control flags
to sort them out. It's like taking an object reference in the base
class type, and using a switch to check which exact subclass it b

【在 xt 的大作中提到】
:
: this is not quite true. We do have different GUI objects having
: different semantics. For example, the "Back" "Next" "Cancel" "Finish"
: buttons on a wizard. We do it with the same class. The way to do is
: to find out the source of the event, and handle it differently.
: Of course, it takes a linear time complexity to do this. But it won't
: take much time, considering
: the number of objects a frame is usualy very small - there is no reason
: for a reasonable GUI to get that many objects in a single windo

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。