128

In Java, nested classes can be either static or not. If they are static, they do not contain a reference to the pointer of the containing instance (they are also not called inner classes anymore, they are called nested classes).

Forgetting to make an nested class static when it does not need that reference can lead to problems with garbage collection or escape analysis.

Is it possible to make an anonymous inner class static as well? Or does the compiler figure this out automatically (which it could, because there cannot be any subclasses)?

For example, if I make an anonymous comparator, I almost never need the reference to the outside:

 Collections.sort(list, new Comparator<String>(){ int compare(String a, String b){ return a.toUpperCase().compareTo(b.toUpperCase()); } } 
3
  • What are the problems with "garbage collection or escape analysis" when forgetting to make an inner class static? I thought this is about performance only... Commented Jul 6, 2009 at 8:20
  • 17
    Your inner class instance keeps a reference to its outer instance alive, even if you do not need it. This could keep stuff from getting garbage-collected. Picture a (resource-heavy) factory object that creates lightweight instances of something. After the factory has done its work (e.g. during application startup), it could be disposed of, but that only works if the things it has created do not link back. Commented Jul 6, 2009 at 9:34
  • I know, this is only an example, but since it is a recurring one, it should be mentioned that Collections.sort(list, String.CASE_INSENSITIVE_ORDER) works since Java 2, read, since the Collection API exists… Commented Jan 16, 2017 at 15:59

6 Answers 6

141

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.1). 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 be 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.

Sign up to request clarification or add additional context in comments.

11 Comments

+1 for FindBugs - every Java developer should have this in their build.
That is very unfortunate, because it means you may want to avoid this otherwise almost concise syntax for performance reasons.
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.
Here's an example of how it's implementation dependent: this code prints true using javac (sun-jdk-1.7.0_10) and false using Eclipse compiler.
@MichaelMyers I have tryed to simulate the FindBugs alerting me of doing an Anonymous Inner without using the 'this' reference, and nothing happens. Can you demonstrate how FindBugs alert you as you said in the begining of your answer? Just paste some link or what ever.
|
16

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!

4 Comments

"(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.
We're actually not talking about any of the patterns. We mean that the anonymous nested class is static. I.e. add a "static" between new and JComponent in your third example.
I added a clarification to the original question to show what is wanted.
@MichaelMyers, The dictation in JLS always needs to be interpreted.
15

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.

3 Comments

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."
static in a different sense to that in the question.
I;ve added a little clarification.
6

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

1 Comment

I have updated the question with a reference to the official nomenclature.
0

anonymous inner classes are never static (they can't declare static methods or non final static fields),but if they're defined in a static context (static method or static field) they behave as static in the sense that they can't access non-static (i.e. instance) members of the enclosing class (like everything else from a static context)

Comments

-3

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 Comment

-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.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.