Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4489635
Votes 1
Synopsis Access to outer object of non-static inner object other than 'this'
Category java:specification
Reported Against 1.3.1
Release Fixed
State 11-Closed, Will Not Fix, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 08-AUG-2001
Description




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  customer  of the
'this'  customer  of a non-static inner class type (e.g., 'Outer.this'), there is no
syntax for accessing the outer  customer  of an inner  customer  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  customer  of a non-this  customer :

            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:

     customer .Classname.this

(or something like it) which retrieves the outer class  customer  (of type
'Classname') of  customer  ' customer ', 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) 
======================================================================
Work Around




(See the example code.)
======================================================================
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.

  xxxxx@xxxxx   2002-12-16
Comments
  
  Include a link with my name & email   

Submitted On 06-SEP-2001
davidtribble
A minor correction to the example code:

    static int compare(Outer a, Outer b)
    { ... }


Submitted On 14-SEP-2001
sleung1
A similar bug:

public class Outer {
    public int x;

    public class Inner {
        public int y;

        public boolean equals(Object object) {
            if (object instanceof Inner) {
                Inner inner = (Inner) object;
                return y == inner.y && x == inner.x; // [1]
            }
            return false;
        }
    }
}

In the above case, "javac" cannot resolve the "x" 
in "inner.x" at [1], whose meaning I think can be 
determined from the context. 



PLEASE NOTE: JDK6 is formerly known as Project Mustang