interfaces & inner classes

L

led

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!
 
O

Oliver Wong

led said:
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
 
M

Matt Humphrey

led said:
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 (e-mail address removed) http://www.iviz.com/
 
L

led

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..
 
M

Mike Schilling

led said:
WOW...thx dude...


this piece of code explained me a lot of thing...
but so far, this is gonna wait for some time!!



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.
 
D

Dijon Yu

led said:
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!

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.
 
T

Tor Iver Wilhelmsen

led said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,081
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top