2

I know it depends on the context in which the anonymous class has been written (static or non static method). but look this part of code:

public class A {
    int fieldOfA;

    private static class B {
        int fieldOfB;
    }

    public static void main(String[] args) {

        B obj = new B() { //this anonymous class is static becuase is in the main method.

            private static void testMethod() { //so why here i have an error and i can put just a non-static method
                                               //if my class is static ?
                                               //a class static can have static method, but this class no, why?
            }
        };
    }
}

it's sure that anonymous class are static?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Giovanni Far
  • 1,623
  • 6
  • 23
  • 37
  • 4
    The class is not static, it is local to the main function. Where did you find that definition ? – Sage Dec 12 '13 at 21:45
  • We already had this battle about 4 days ago. – Hot Licks Dec 12 '13 at 21:45
  • Static classes are ones explicitly marked with 'static' keyword. – jdevelop Dec 12 '13 at 21:46
  • And classes marked with the `static` keyword are static. – Hot Licks Dec 12 '13 at 21:47
  • @Sage here http://stackoverflow.com/questions/758570/is-it-possible-to-make-anonymous-inner-classes-in-java-static – Giovanni Far Dec 12 '13 at 21:48
  • The problem is that the JLS apparently defines a "static class" as one that has the `static` attribute specified in its declaration. The fact that a class looks, smells, and acts static is irrelevant, if it ain't got that attribute. – Hot Licks Dec 12 '13 at 21:51
  • @GiovanniFar, I think you are misunderstanding about nested inner class and anonymous class. And a nested class declared with static is not considered as inner class – Sage Dec 12 '13 at 22:00
  • Would be a duplicate of this one if it hadn't been closed: http://stackoverflow.com/questions/20446660/java-inner-class-not-static – Hot Licks Dec 12 '13 at 23:19
  • no it isnt, here the question is different. – Giovanni Far Dec 12 '13 at 23:26

2 Answers2

5

An anonymous class is static if the context is static. e.g. in a static method.

An anonymous class is non static if there is a non static context, whether you need it to be non-static or not. The compiler is not smart enough to make a class static if the non static context is not used.

In this example, two anonymous classes were created. One in a static method has no reference to an outer class and is like a static nested class.

Note: these classes are still called "Inner" and cannot have static members even though they have no reference to an Outer class.

import java.util.Arrays;

public class Main {
    Object o = new Object() {
        {
            Object m = Main.this; // o has a reference to an outer class.
        }
    };

    static Object O = new Object() {
        // no reference to Main.this;
        // doesn't compile if you use Math.this
    };

    public void nonStaticMethod() {
        Object o = new Object() {
            {
                Object m = Main.this; // o has a reference to an outer class.
            }
        };
        printFields("Anonymous class in nonStaticMethod", o);
    }

    public static void staticMethod() {
        Object o = new Object() {
            // no reference to Main.this;
            // doesn't compile if you use Math.this
        };
        printFields("Anonymous class in staticMethod", o);
    }

    private static void printFields(String s, Object o) {
        System.out.println(s + " has fields " + Arrays.toString(o.getClass().getDeclaredFields()));
    }

    public static void main(String... ignored) {
        printFields("Non static field ", new Main().o);
        printFields("static field ", Main.O);
        new Main().nonStaticMethod();
        Main.staticMethod();
    }
}

prints

Non static field  has fields [final Main Main$1.this$0]
static field  has fields []
Anonymous class in nonStaticMethod has fields [final Main Main$3.this$0]
Anonymous class in staticMethod has fields []
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    [Java prohibits `static` modifier for all inner classes of anonymous classes either way](http://ideone.com/j2A3aD). – Sergey Kalinichenko Dec 12 '13 at 21:53
  • @Peter sorry i dont understand your answer – Giovanni Far Dec 12 '13 at 21:55
  • 1
    The JLS disagrees with this. – telkins Dec 12 '13 at 21:57
  • @GiovanniFar I have added four examples, two where the anonymous class was defined in a static context and has no reference to an outer class instances, and two anonymous classes where the class has an implicit reference to an instance of an outer class. – Peter Lawrey Dec 12 '13 at 22:18
  • Examples are ok, but smaller `o` and bigger `O` got into the way to understand it easily. :( – Sage Dec 12 '13 at 22:28
  • @PeterLawrey thx this i understand, but i dont understand why when my anonymous inner clas is static i cannot put static fields or method. this i cannot understand – Giovanni Far Dec 12 '13 at 22:29
  • @PeterLawrey Your assertion that "An anonymous class is static if the context is static" is wrong, according to the JLS. It may "act" static in that its statements and expressions have a static context -- but it is _not_ static, which is why Giovanni Far can't put static fields or methods in it. You may think it's a nit on my part, but in this case it's a crucial nit, as it goes directly to the question of why this anonymous class, which otherwise seems static-ish, has a restriction that static classes don't have. The answer is that it's not static. – yshavit Dec 12 '13 at 23:35
2

From JLS 15.9.5:

An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

Section 8.1.3 talks more about inner classes, including when they occur in a static context. But they're never static themselves, and thus can't declare static members (other than constant variables).

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • this i reed, and so why some people says that depends in which context is the anonymous class? – Giovanni Far Dec 12 '13 at 21:57
  • @GiovanniFar I just edited my answer a bit. Statements and expressions within the anonymous class may have a static context, if the anonymous class is declared within a static context (see 8.1.3). But the anonymous class itself is never static. Some people may say otherwise, but they're wrong, because the JLS is by definition right. :) – yshavit Dec 12 '13 at 22:01