Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Karina K patni   on Aug 04 In Java Category.

  
Question Answered By: Gustavo Taylor   on Aug 04

A little more complete version of the code in question:

class A
{
int test() { return  1; }
class B
{
int test() { return A.this.test(); } // this is the proxy I want
to do without
}
}

class C extends A.B
{
C(A a)
{
a.super();
}

void stuff()
{
System.out.println(test());
}

public static void main(String[] args)
{
A a = new A();
C c = new C(a);
c.stuff();
}
}

Ok, my appologies for the earlier confusing version.

> You are correct, I didn't bother compiling it. After all, it
doesn't actually do anything.

In order to concentrate on the issue at hand, I reduced a larger
scenario to a minimal set of classes.

> You didn't respond to my main point - i.e. the use of "a.test ()"
which seemed to me to be the best way to access A's test. Did this work?

In my previous sample I could have called a.test(), as you pointed
out. This is not what I ment however. I changed the sample code so
that I try to call  from a method where 'a' is not available, unless
manually stored.

> I hold to my point about "a.super()" not being valid / relevant code.

Try to comment out the a.super() call in C, you'll get an error:
'an enclosing instance that contains A.B is required'
or from jikes:
'Semantic Error: An instance of "A.this" is not accessible here. In
general, an enclosing instance is accessible only in the body of an
instance method, constructor (after the explicit constructor
invocation, if any), initializer block, or in the initializer
expression of an instance variable.'

The issue I wanted to get at, is that the 'test' proxy method in 'B'
is needed to make a 'test' call possible from 'C'.

Commenting out the 'test' proxy call in 'B' results in another
compiler error  (jikes):
'Semantic Error: No method named "test" was found in type "C".'

> It looks weird  because it is unusual. Notice the lack of responses
from any of the java  gurus on this list. Post the actual requirement
and you'll probably be inundated with help on the "real" way to do it.

I'll try to be more complete and clear next time.

It's not a useless scenario though. One could have a base service of
sorts that's represented in an inner class, enclosed by a managing
outer class. Then there could be a set of specialized classes that
derive from the inner service class.

Anyway, now that hopefully, it is more clear now, do you or anyone
else know how to call outer class  A's 'test' method directly from
derived inner class C's 'stuff', without storing any reference to A
(a), or any proxy method like I had to do?

Share: 

 

This Question has 18 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Inner class inherit question Or get search suggestion and latest updates.


Tagged: