United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4489635 : Access to outer object of non-static inner object other than 'this'

Details
Type:
Enhancement
Submit Date:
2001-08-08
Status:
Closed
Updated Date:
2002-12-16
Project Name:
JDK
Resolved Date:
2002-12-16
Component:
specification
OS:
generic
Sub-Component:
language
CPU:
generic
Priority:
P4
Resolution:
Won't Fix
Affected Versions:
1.3.1
Fixed Versions:

Related Reports

Sub Tasks

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.)
======================================================================
                                     
2004-06-11
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
                                     
2002-12-16



Hardware and Software, Engineered to Work Together