0

I have compiled the following code (Methods and variables are elided for brevity):

// Outer.java
public class Outer
{
    private class Inner
    {
    }
    void someMethod()
    {
        Inner inObj = this.new Inner();
    }
    public static void main(String s[])
    {
        Outer outerObj = new Outer();
    }
}

When I listed the classes created, it displayed the following:

Outer$1.class
Outer$Inner.class
Outer.class

Outer and Outer$Inner appear logical. What is the purpose of Outer$1 class? What is the order of creation of these in time scale?

Seshadri R
  • 1,192
  • 14
  • 24
  • 1
    "What is the order of creation of these in time scale?" Why do you think it is important to know that? – Andy Turner Dec 01 '17 at 08:55
  • Why not? It is to understand the javac's mechanism of creating classes. – Seshadri R Dec 01 '17 at 08:58
  • Order seems to be `1. Outer.class 2. Outer$Inner.class 3. Outer$1.class`, when sorted by `date modified` column in Windows. – 11thdimension Dec 01 '17 at 09:00
  • 3
    When would the order in which class files are created ever matter? Whether and when instances of classes are created at execution time would be relevant, but when the class files are created seems somewhat irrelevant. – Jon Skeet Dec 01 '17 at 09:03
  • In this case it matters since the curious creation of Outer$1.class will be somewhat demystified. – Seshadri R Dec 01 '17 at 09:06
  • 1
    [Oak's answer](https://stackoverflow.com/a/2883541/3788176) to the dupe explains it, sort of. – Andy Turner Dec 01 '17 at 09:09
  • I'm not sure that's a complete answer, because JRE doesn't seem to mind if this extra class is missing. It's only generated by compiler and everything still works even if it's deleted. – 11thdimension Dec 01 '17 at 09:19

1 Answers1

1

Curious. I'm not sure what this is for. But if you decompile the classes, you can see how it is used:

public class Outer {
  public Outer();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  void someMethod();
    Code:
       0: new           #2                  // class Outer$Inner
       3: dup
       4: aload_0
       5: aconst_null
       6: invokespecial #3                  // Method Outer$Inner."<init>":(LOuter;LOuter$1;)V
       9: astore_1
      10: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #4                  // class Outer
       3: dup
       4: invokespecial #5                  // Method "<init>":()V
       7: astore_1
       8: return
}

class Outer$Inner {
  final Outer this$0;

  Outer$Inner(Outer, Outer$1);
    Code:
       0: aload_0
       1: aload_1
       2: invokespecial #1                  // Method "<init>":(LOuter;)V
       5: return
}

class Outer$1 {
}

So, Outer$1 seems to contain nothing - not even a default constructor. But a reference to a Outer$1 has to be passed to Outer$Inner to construct it. Mysteriously, the value passed in someMethod is null (line 5 in someMethod).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243