Java Delegates, Inner Classes & final

C

cutthecrap

I was wondering how many other programmers have discovered an under
publicised feature of inner classes that makes them generically more
powerful than simple Delegates?

This is that using the "final" keyword for any parameters and variables
declared in a method allows any inner classes read-only access to those
variables in their method overides.

Checkout my original paper at:

http://www.cutthecrap.biz/software/delegates.html

For more details.

Any comments?
 
T

Tony Morris

cutthecrap said:
I was wondering how many other programmers have discovered an under
publicised feature of inner classes that makes them generically more
powerful than simple Delegates?

This is that using the "final" keyword for any parameters and variables
declared in a method allows any inner classes read-only access to those
variables in their method overides.

Checkout my original paper at:

http://www.cutthecrap.biz/software/delegates.html

For more details.

Any comments?

I don't get it? What's under-publicised? Everyone (worth their salt at
least) knows that an inner class doesn't have access to non-final locals. Or
to be more formal, anyone who holds the Sun Certified Programmer for the
Java 2 Platform (the first certification for J2SE) knows the well-publicised
Java Language Specification Second Edition well enough to know that
fundamental rule.

Is there something I'm missing?
Java solves what delegates solve not with inner classes, but with callbacks.
Is this (slightly misinterpreted) news?
 
B

bugbear

Tony said:
..
..
I don't get it? What's under-publicised? Everyone (worth their salt at
least) knows that an inner class doesn't have access to non-final locals.

Heh. If you try to access a non-final local the compiler
TELLS you about this "under publicised" feature,

You don't even need to read a manual.

BugBear
 
C

Chris Uppal

Tony said:
I don't get it? What's under-publicised? Everyone (worth their salt at
least) knows that an inner class doesn't have access to non-final locals.

I think CTC's point is that the inner class /does/ have access to final locals.
It isn't necessarily obvious to a beginner that this is the case.

Mind you, I agree that all competent Java programmers do know it -- or at least
they bloody-well /ought/ to...

Or to be more formal, anyone who holds the Sun Certified Programmer for
the Java 2 Platform (the first certification for J2SE) knows the
well-publicised Java Language Specification Second Edition well enough to
know that fundamental rule.

You surely aren't trying to suggest that all competent Java programmers are Sun
certified !? ;-)

(Or even give a damn about Sun's certification programs, come to that -- but
that's another debate for another day)

--- chris
 
C

cutthecrap

Yep, sure many programmers know that they don't have access to non-final
vairables.

The point is not simply that the inner class does have access to final
variables, but what this enables.

I personally have seen little (no) documentation that demonstrates how
this can enable simple parameterisation of inner class methods.

Maybe Tony and Bugbear can point me at reference?

But my own experince talking with other Java programmers is that it is not
a technique that they are familiar with - and that includes University
researchers as well as "professional" certified programmers.
 
C

cutthecrap

Oh yes, and the comment about callbacks?

A Callback is in no way equivalent to a Delegate. With Delegates - and
java Inner Classes - the whole point is that you can arrange for some
"Callback class" to "Delegate" the required behaviour to some other
method.

The point about using final parameter/variables is that this behaviour can
be more conveniently parameterized.
 
T

Tony Morris

You surely aren't trying to suggest that all competent Java programmers are Sun
certified !? ;-)

(Or even give a damn about Sun's certification programs, come to that -- but
that's another debate for another day)

Not at all.
The debate has been had - I have arrived at the conclusion that debating
with someone without the certification is pointless.
Analogous to debating the good ol' "Java is slow" myth with someone who has
never used Java - you only receive erroneous and flaky arguments often based
on misinformation.

It is fair to say that a SCJP has a fundamental knowledge of the JLS 2nd Ed.
But I certainly wasn't saying that this implies (or doesn't imply) the
ability to use the technology competently.
 
T

Tony Morris

cutthecrap said:
Yep, sure many programmers know that they don't have access to non-final
vairables.

The point is not simply that the inner class does have access to final
variables, but what this enables.

I personally have seen little (no) documentation that demonstrates how
this can enable simple parameterisation of inner class methods.

Maybe Tony and Bugbear can point me at reference?

But my own experince talking with other Java programmers is that it is not
a technique that they are familiar with - and that includes University
researchers as well as "professional" certified programmers.

Then they aren't "worth their salt" :)

JLS §8.1.2

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#228205

"Any local variable, formal method parameter or exception handler parameter
used but not declared in an inner class must be declared final, and must be
definitely assigned (§16) before the body of the inner class"
 
C

cutthecrap

I was, and a C# delegate is an object dispatch to a method signature.

The equivalent to this in java is through use of an anonymous inner class
- one that implements the required interface - that "delegates" the
request on to some method.

- although as a "SCJP" I'm sure you are well aware of the distinction :)

Sorry Tony, but my original note was not to present "new information" on
the fact that it *was not* possible to access non-final variables, or even
that it *was* possible to access final variables. The intention was to
encourage programmers to realise how the use of final variables could be
applied with inner class definitions. My experience is that even with
those who apparently know the rules they do not use the technology
effectively.

I think most would agree that a thorough understanding of class
inheritance and interface implementations does not mean that a programmer
will produce a well designed Object Oriented system, even if it is a
system apparently populated by many Objects.

Likewise, understanding the lexical constraints on variable use does not
mean that the programmer has an appreciation for how such features can be
used.

I can see I'm going to have to try harder :)
 
C

cutthecrap

Hey, guess what, I did know that only "final" variables were allowed.

In fact, I found it in some java documentation, and not on a SCJP course
:).

But I was really looking for a more extended example that *emphasised* the
use of final variables.

..anyway, enough already, this has ended up as a discussion about who
should know about some syntax rule, while I was interested in a discussion
on the effective functional application of inner classes and functional
comparison with C# delegates.

Does anyone have anything positive to contribute?
 
C

Chris Uppal

Not at all.
The debate has been had - I have arrived at the conclusion that debating
with someone without the certification is pointless.

Ah, then you'll not be wanting to debate with me.

(Only joking).

But seriously, do you find that even a small percentage of the knowledgeable
(enough to be competent if they also have the talent) Java programmers hold
Sun certificates ? That is not my experience.

-- chris
 
C

Chris Uppal

Cid said:
Woah, never put it together before, but is this expression derived
from Roman soldiers being paid in salt?

According to my edition of Brewer's it is.

(Although I am inclined to take that cum grano salis -- salt has been so
central to Western life for so long that it appears in all sorts of figurative
applications, and although the derivation is very tempting, it's odd that the
OED has no record of the phrase being used before 1830. But then, I am not an
expert.)

-- chris
 
T

Tony Morris

I personally have seen little (no) documentation that demonstrates how
this can enable simple parameterisation of inner class methods.

Maybe Tony and Bugbear can point me at reference?

Hey, guess what, I did know that only "final" variables were allowed.

Seems like a contradiction.
Your intention is becoming less clear.

Side Note: There is no "SCJP course" (actually, I believe there is, but I,
and others I know, know little about it).
I learnt about the language specifics of Java by reading the language
specification (as I am doing for C# when I get a spare moment). Most of my
work is "J2EE-centric", so I don't spend much time thinking about language
details - it's good to know it without the need to reference (or at least,
not often).

Good luck mate.
 
T

Tony Morris

But seriously, do you find that even a small percentage of the
knowledgeable
(enough to be competent if they also have the talent) Java programmers hold
Sun certificates ? That is not my experience.

I know many who don't hold any certificates.
Each certificate proves a certain level of understanding.
For example, the SCJP proves the ability to memorise the JLS (or at least,
parts of) - this helps, but it certainly does not reflect the set of
pre-requisites to use the technology effectively. The SCJD demonstrates the
ability to use the J2SE and design software accordingly (distinct from the
J2EE design patterns). Again, still not entire coverage of the necessary
pre-requisites. And then, you get the people who do the Sun training course
(can't help but think there's some bias there), and those who buy their
certification from dodgycertificates.com (or whatever).

I work on a team of ~50 people.
I hold the SCJP and SCJD.
Another holds the SCJP (1.2) and SCWCD.
I consider this other person one of the most competent on the team.
I consider perhaps 10 others closely behind.
20 or so don't even need to use Java/J2EE, and so have little or no
knowledge.
I am the team "code Nazi" (I despise the term, but that I am) - all code is
reviewed by me before it leaves the door.

You might be able to draw a conclusion from that - be it known that I didn't
mean to imply anything like is now being speculated.
 
C

Chris Uppal

Tony said:
I work on a team of ~50 people.
[... data snipped...]
You might be able to draw a conclusion from that - be it known that I
didn't mean to imply anything like is now being speculated.

No conclusion; it sounds as if the demographics where you are do not differ
markedly from what I've found. Your comments had lead me to suspect that
certification might be a lot more common in your environment.

Cheers.

-- chris
 
B

bugbear

cutthecrap said:
Yep, sure many programmers know that they don't have access to non-final
vairables.

The point is not simply that the inner class does have access to final
variables, but what this enables.

I personally have seen little (no) documentation that demonstrates how
this can enable simple parameterisation of inner class methods.

Maybe Tony and Bugbear can point me at reference?

This sort of thing?
http://java.sun.com/docs/books/tutorial/uiswing/events/generalrules.html#innerClasses

BugBear
 

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
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top