I came across this piece of code while I was working on a project, this was reported as a bug for something.
public class Test { public static Test ONE = new Test() { @Override public int value() { return 1; } }; private Test() { } public int value() { return 0; } }
What I think this code means:
I think that this is simply instantiating the class Test
with a reference variable named ONE
.
What the user mentioned:
The user mentioned that this is a nested class within the outer class, but I’m confused that how can a class be nested within itself.
What I have researched:
I used code parser named ANTLR to parse this code and it showed that this is not a class, insted used the token Type.
Thanks in Advance
Answer
It’s an (Nested) Anonymous Inner Class, but not what people typically call a Nested Class which, as far as I’ve noticed, usually refers to the non-local non-anonymous member classes. Though any class which isn’t a Top-Level Class is of course a Nested Class.
The class is not “nested within itself” since new Test() { ... }
is actually an anonymous subclass of Test
.
I’ve included a diagram I found here and a pretty exhaustive list of examples in code.
import static java.lang.System.out; @SuppressWarnings("unused") public class Test { static String staticField = "field in class static context"; String instanceField = "field in instance context"; static Object staticRef1 = /* Static Field Reference to ... */ /* ... Anonymous Inner Class created in static context */ new Object() { public void print() { out.println(staticField); } }; static Object staticRef2; /* Static Field Reference to ... */ static { /* static block ... */ String localVar_InStaticBlock = "local variable in static block context"; /* ... Anonymous Inner Class created in static block context */ staticRef2 = new Object() { public void print() { out.println(localVar_InStaticBlock); out.println(staticField); } }; /* Local Reference to Anonymous Inner Class created in static block context */ Object localVar = new Object() { public void print() { out.println(localVar_InStaticBlock); out.println(staticField); } }; }
public Object instanceRef1 = /* Instance Field reference to ... */ /* ... Anonymous Inner Class created in instance context */ new Object() { public void print() { out.println(instanceField); out.println(staticField); } }; /* Instance Field reference to ... */ public Object instanceRef2; { /* instance block... */ String localVar_InInstanceBlock = "local variable in instance block context"; /* ... Anonymous Inner Class created in instance block context*/ instanceRef2 = new Object() { public void print() { out.println(localVar_InInstanceBlock); out.println(instanceField); out.println(staticField); } }; /* Local Reference to Anonymous Inner Class created in instance block context */ Object localVar = new Object() { public void print() { out.println(localVar_InInstanceBlock); out.println(instanceField); out.println(staticField); } }; }
/* Nested Inner Class */ class NestedInnerClass { public void foo() { out.println(instanceField); out.println(staticField); } } /* Nested Static Class */ static class NestedStaticClass { public void foo() { out.println(staticField); } }
void method() { String localVar_InMethodContext = "local variable in method context"; /* Method-Local Inner Class */ class MethodLocalInnerClass { void print() { out.print(localVar_InMethodContext); out.println(instanceField); out.println(staticField); } } Object methodLocalTest = new MethodLocalInnerClass(); // ... } }