Is it possible to make anonymous inner classes in Java static?

No, you can't, and no, the compiler can't figure it out. This is why FindBugs always suggests changing anonymous inner classes to named static nested classes if they don't use their implicit this reference.

No, you can't, and no, the compiler can't figure it out. This is why FindBugs always suggests changing anonymous inner classes to named static nested classes if they don't use their implicit this reference. Edit: Tom Hawtin - tackline says that if the anonymous class is created in a static context (e.g.In the main method), the anonymous class is in fact static.

But the JLS disagrees: An anonymous class is never abstract (§8.1.1.1). An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.2). An anonymous class is always implicitly final (§8.1.1.2).

Roedy Green's Java Glossary says that the fact that anonymous classes are allowed in a static context is implementation-dependent: If you want to baffle those maintaining your code, wags have discovered javac. Exe will permit anonymous classes inside static init code and static methods, even though the language spec says than anonymous classes are never static. These anonymous classes, of course, have no access to the instance fields of the object.

I don’t recommend doing this. The feature could be pulled at any time. Edit 2: The JLS actually covers static contexts more explicitly in §15.9.2: Let C be the class being instantiated, and let I the instance being created.

If C is an inner class then I may have an immediately enclosing instance. The immediately enclosing instance of I (§8.1.3) is determined as follows: If C is an anonymous class, then: If the class instance creation expression occurs in a static context (§8.1.3), then I has no immediately enclosing instance. Otherwise, the immediately enclosing instance of I is this.

So an anonymous class in a static context is roughly equivalent to a static nested class in that it does not keep a reference to the enclosing class, even though it's technically not a static class.

7 +1 for FindBugs - every Java developer should have this in their build. – Andrew Duffy Apr 17 '09 at 1:00 1 That is very unfortunate, because it means you may want to avoid this otherwise almost concise syntax for performance reasons. – Thilo Apr 17 '09 at 1:45 1 JLS 3rd Ed deals with the case of inner classes in static contexts.

They are not static in the JLS sense, but the are static in the sense given in the question. – Tom Hawtin - tackline Apr 17 '09 at 14:33.

Inner classes can't be static - a static nested class is not an inner class. The Java tutorial talks about it here.

I have updated the question with a reference to the official nomenclature. – Thilo Apr 17 '09 at 1:43.

Kind of. An anonymous inner class created in a static method will obviously be effectively static because there is no source for an outer this. There are some technical differences between inner classes in static contexts and static nested classes.

If you're interested, read the JLS 3rd Ed.

Oh, very good point. – Michael Myers? Apr 17 '09 at 12:22 Actually, I take that back; the JLS disagrees.Java.sun.Com/docs/books/jls/third%5Fedition/html/…: "An anonymous class is always an inner class ; it is never static.

" – Michael Myers? Apr 17 '09 at 12:44 1 static in a different sense to that in the question. – Tom Hawtin - tackline Apr 17 '09 at 14:28 1 I;ve added a little clarification.

– Tom Hawtin - tackline Apr 17 '09 at 14:35.

I think there's a bit of confusion in the nomenclature here, which admittedly is too silly and confusing. Whatever you call them, these patterns (and a few variations with different visibility) are all possible, normal, legal Java: public class MyClass { class MyClassInside { } } public class MyClass { public static class MyClassInside { } } public class MyClass { public void method() { JComponent jc = new JComponent() { ... } } } public class MyClass { public static void myStaticMethod() { JComponent jc = new JComponent() { ... } } } They are catered for in the language spec (if you're really bothered, see section 15.9.5.1 for the one inside the static method). But this quote is just plain wrong: javac.

Exe will permit anonymous classes inside static init code and static methods, even though the language spec says than anonymous classes are never static I think the quoted author is confusing the static keyword with static context. (Admittedly, the JLS is also a bit confusing in this respect. ) Honestly, all of the patterns above are fine (whatever you call them "nested", "inner", "anonymous" whatever...).

Really, nobody is going to suddenly remove this functionality in the next release of Java. Honestly!

(Admittedly, the JLS is also a bit confusing in this respect. )" You got that right. It sounded strange to say that it depends on the implementation, but I don't recall having seen any obvious errors in the Java Glossary before.

From now on, I take it with a grain of salt. – Michael Myers? Apr 17 '09 at 15:17.

On the note of making an anonymous inner class static by calling them within a static method. This doesn't actually remove the reference. You can test this by trying to serialize the anonymous class and not making the enclosing class serializable.

1: Creating an anonymous class within a static method actually does remove the reference to the outer class. You can test this by trying to serialize the anonymous class and not making the enclosing class serializable. (I just did.) – Christian Semrau Jun 24 at 20:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions