JDK-4489635 : Access to outer object of non-static inner object other than 'this'
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.3.1
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2001-08-08
  • Updated: 2002-12-16
  • Resolved: 2002-12-16
Description

Name: bsC130419			Date: 08/08/2001


java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)


While current Java syntax allows access to the outer containing object of the
'this' object of a non-static inner class type (e.g., 'Outer.this'), there is no
syntax for accessing the outer object of an inner object other than 'this'.

For example:

    class Outer                 // Outer class
    {
        int compare(Outer o)
        {...}

        class Inner             // Non-static inner class
        {
            int compare(Inner i)
            {
                // Compare the outer objects of 'this' and 'i'
                return Outer.compare(Outer.this, i.???);        // [A]
            }
        }
    }

With the present language syntax, I am forced to use a helper method in the
inner class:

        class Inner
        {
            Outer thisOuter(Inner)
            {
                return this.Outer;        // [B]
            }
            ...
        }

This hack allows me to write the method of the inner class that needs to access
the outer object of a non-this object:

            int compare(Inner i)
            {
                // Compare the outer objects of 'this' and 'i'
                return Outer.compare(Outer.this, i.thisOuter());   // [C]
            }

But the overhead of a helper function should not be necessary.  I feel that the
Java language syntax should be extended to allow expressions of the form:

    object.Classname.this

(or something like it) which retrieves the outer class object (of type
'Classname') of object 'object', which is of some non-static inner class type.
This syntax is very close in spirit to the existing syntax:

    Classname.this

--
(Review ID: 129580) 
======================================================================

Comments
WORK AROUND Name: bsC130419 Date: 08/08/2001 (See the example code.) ======================================================================
11-06-2004

EVALUATION Whether an object exports its enclosing objects is up to the object. It would be wrong for the language to forcibly expose this information in the manner suggested here. Besides, Java inner classes are already more complex than we would like, an adding features to them would be unadvisable. ###@###.### 2002-12-16
16-12-2002