Re: "Hello world!" without a public class?
On 1/6/2013 4:21 PM, Aryeh M. Friedman wrote:
On Sunday, January 6, 2013 4:07:17 PM UTC-5, Arne Vajh?j wrote:
Given that the above is both unreadable and un-compilable, then ...
Ok here is some actual working code that effectively does the same thing with the psedocode filled in (including the command lines):
% cat Foo.java
public enum Foo
{
ACK {
public String doSomething(String arg)
{
return "ACK: "+arg;
}
},
BAR {
public String doSomething(String arg)
{
return "BAR: "+arg;
}
};
public abstract String doSomething(String arg);
}
% cat Fred.java
public class Fred
{
public static void main(String[] args)
{
for(Foo elem:new Foo[]{Foo.ACK,Foo.BAR})
System.out.println(elem.doSomething("I am doing something"));
}
}
% javac Fred.java
% java Fred
ACK: I am doing something
BAR: I am doing something
Now please tell me that does not replace inner classes (the enum constants are compiled as inner classes and not just straight data constants).
Well - now it compiles.
But:
1) you have made the code a lot more difficult to read by using
an enum for something non-enumish, and the same effect can
be had using normal classes just with a few more keywords
2) it is only relevant for static nested classes as it does not
provide the capturing the non-static nested classes do
3) it is only relevant for static nested classes that implements
not use that extends due to limitations on enum
In other words: no beef.
Arne