Options
class inside static method
![poguy](https://us.v-cdn.net/6030959/uploads/defaultavatar/nE1RZK93BLG3K.jpg)
public class Test {
public static void main(String args[]) {
class Foo {
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println(“i = “ + foo.i);
}
}
So Foo is a static class, how come it can init an instance from static class?
thank you
public static void main(String args[]) {
class Foo {
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println(“i = “ + foo.i);
}
}
So Foo is a static class, how come it can init an instance from static class?
thank you
Comments
-
Options
JDMurray Admin Posts: 13,058 Admin
That code won't compile with Java 1.6. Try this instead:class Test { [B][I]static [/I]class Foo { public int i = 3; }[/B] public static void main(String args[]) { Object o = (Object)new Foo(); Foo foo = (Foo)o; System.out.println("i = " + foo.i); } }
As I understand it, instantiating static classes is legal and allowed in Java, but apparently there is no performance or access advantage in doing it.