Re: "Hello world!" without a public class?
Aryeh M. Friedman wrote:
On inner classes:
...
(yes it does get compiled into something very close to inner classes [not=
nested if I remember the jargon correctly]
Terminology: A nested type (class or interface) is a member of another typ=
e.
(JLS =A78.5)
There are two kinds of nested classes, static and inner, per the JLS.
By definition, an inner class is a nested class whose declaration does not =
include the keyword 'static'.
"The static keyword may modify the declaration of a member type C within th=
e
body of a non-inner class or interface T. Its effect is to declare that C i=
s
not an inner class."
(JLS =A78.5.1)
but no where near as ugly):
These get compiled exactly into inner classes.
Foo.java:
public enum Foo
{
ACK {
@Override
public void doSomething(SomeClass arg)
{
do what ever for ACK
}
},
...
}
In this case the enum, itself a cover for compiler-supported boilerplate fo=
r a
class with static constant members, also covers for the boilerplate of inne=
r
class declarations in those constants.
Much as the lambdas I mentioned earlier cover for the boilerplate of inner-
class declarations of SAM interface implementations.
Neither of these work without inner classes, so your proposal to eliminate=
inner classes would break them.
Fred.java:
...
and voila much more readable then putting a whole rats nest of stuff in t=
he initialization of arr
Perhaps, but it doesn't prove the point, because it relies on inner classes=
..
And your claim that it's "much more readable" is not really proven.
public abstract class Bar
{
private final String rep;
abstract public void doSomething(Some arg);
private Bar(String r)
{
this.rep = r;
}
public static final BAR = new Bar("BAR")
{
@Override
public void doSomething(Some arg)
{
// do what ever for BAR
}
};
}
The only part of the anonymous class declaration that differs is the
new Bar("BAR")
Not even "much" different in readability, let alone "very much".
--
Lew