Re: "Hello world!" without a public class?
On Sunday, January 6, 2013 4:07:17 PM UTC-5, Arne Vajh=F8j wrote:
On 1/6/2013 1:48 PM, Aryeh M. Friedman wrote:
Now that being said here is a good example of something that is easier=
to read then either (yes it does get compiled into something very close=
to inner classes [not nested if I remember the jargon correctly] but no=
where near as ugly):
Foo.java:
public enum Foo
{
ACK {
public void doSomething(SomeClass arg)
{
do what ever for ACK
}
},
BAR {
public void doSomething(SomeClass arg)
{
do what ever for BAR
}
}
}
Fred.java:
public class Fred
{
public void someMethod()
{
SomeClass sc=new SomeClass(...);
Foo[] arr=new Foo[]{ACK,BAR}; // typing this for this examp=
le so being lazy on syntax
for(Foo elem:arr)
elem.doSomething(sc);
}
}
and voila much more readable then putting a whole rats nest of stuff in=
the initialization of arr
}
}
Given that the above is both unreadable and un-compilable, then ...
Arne
Ok here is some actual working code that effectively does the same thing wi=
th 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).