hello..
I'm a beginner in Java, so I'm learning from "Thinking in Java" by BE
I've came to the chapter interfaces & inner classes
so my quest. is: how often do you use inner classes?
they simply don't make sesnse to me...or am I wrong? and
should I study them much deeper?
thank you!
Oliver Wong - 20 Sep 2006 19:22 GMT
> hello..
> I'm a beginner in Java, so I'm learning from "Thinking in Java" by BE
> I've came to the chapter interfaces & inner classes
> so my quest. is: how often do you use inner classes?
> they simply don't make sesnse to me...or am I wrong? and
> should I study them much deeper?
My recommendation: don't worry about inner classes at this point. Just
realize that they exist, and move on with your Java studies. When you start
working with large frameworks or complex APIs, you'll have more background
and experience that might let you understand why inner classes are sometimes
useful.
- Oliver
led - 20 Sep 2006 20:12 GMT
that's exactly what I'm gonna do.. :-))
> My recommendation: don't worry about inner classes at this point. Just
> realize that they exist, and move on with your Java studies. When you
[quoted text clipped - 3 lines]
>
> - Oliver
Matt Humphrey - 20 Sep 2006 19:31 GMT
> hello..
> I'm a beginner in Java, so I'm learning from "Thinking in Java" by BE
> I've came to the chapter interfaces & inner classes
> so my quest. is: how often do you use inner classes?
> they simply don't make sesnse to me...or am I wrong? and
> should I study them much deeper?
They are definately worth using, especially as you move into doing programs
of realistic size and complexity. Anonymous inner classes are widely used
for GUI event handlers because they allow you to specify functionality that
is separate from the GUI class but that will execute in the context of that
class.
myButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent actionEvent) {
// Write your code here that will be executed when the button is pressed
// It can use the instance variables of the enclosing class.
}
});
Notice that the ActionListener is actually an Interface. For some listeners
that have many methods there are default implementations that have empty
methods that you can use so that you only have to provide an implementation
for the one you want, as in using a MouseAdapter or WindowAdapter (check
these out in the API)
I often use named inner classes for specialized GUI data structures, such as
special kinds of JTree nodes or Cell Renderers and so forth.
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
led - 20 Sep 2006 20:11 GMT
WOW...thx dude...
> myButton.addActionListener (new ActionListener () {
> public void actionPerformed (ActionEvent actionEvent) {
> // Write your code here that will be executed when the button is pressed
> // It can use the instance variables of the enclosing class.
> }
> });
this piece of code explained me a lot of thing...
but so far, this is gonna wait for some time!!
> Notice that the ActionListener is actually an Interface. For some
> listeners
well, yes in text BE mentioned that interface & inner classes are quite
similar but yet different..
thx a lot 1 again..
Mike Schilling - 20 Sep 2006 21:42 GMT
> WOW...thx dude...
>
[quoted text clipped - 13 lines]
> well, yes in text BE mentioned that interface & inner classes are quite
> similar but yet different..
I hope it didn't say that; they're not at all similar. But an inner class
is often used to *implement* an interface, so the concepts often arise at
the same time.
Michael Rauscher - 20 Sep 2006 19:32 GMT
led schrieb:
> hello..
> I'm a beginner in Java, so I'm learning from "Thinking in Java" by BE
> I've came to the chapter interfaces & inner classes
> so my quest. is: how often do you use inner classes?
Often.
http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html
Bye
Michael
Dijon Yu - 22 Sep 2006 02:23 GMT
> hello..
> I'm a beginner in Java, so I'm learning from "Thinking in Java" by BE
[quoted text clipped - 4 lines]
>
> thank you!
If you reading the book first, I suggest you can pass it. Because if
you cannt use it in some practice, you won't get the deeper understand.
And when you write some complex GUI program, you will use and know it.
In the first, I think the basic grammer and detail is useful for the
beginner.
Tor Iver Wilhelmsen - 23 Sep 2006 09:24 GMT
> how often do you use inner classes?
Frequently.
Basically, nesting classes inside other classes have two effects:
1) They become nested inside a namespace, thus they don't collide with
other classes which might share the same short name elsewhere.
2) They gain a strong association with their enclosing class that
other classes lack.
> they simply don't make sesnse to me...or am I wrong? and
> should I study them much deeper?
You should; Nested (member) classes, whether inner classes or static,
are just as important members as methods or fields - and you do learn
about those, right? :)
The problem is that they are just "member" classes for the purposes of
the language; once compiled they are synthesized into top-level
classes with some magic regarding visibility and access.