Options

class inside static method

poguypoguy Member Posts: 91 ■■□□□□□□□□
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

Comments

  • Options
    JDMurrayJDMurray 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.
Sign In or Register to comment.