• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

accessability of outerclass members in inner class !!!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody
class A
{
void print() {}
void print(int val) {}
class B
{
void print() { }
void show()
{
print(); //same as this.print();
print(1); //same as this.print(1); --LINE1
}
}
}
the above code gives error that method
print(1) is not found in class B.
whereever i know that all members of outer class
are accessible to inner class so why it is
giving error.

case1)if i donot define print() method in class B
and call both methods of A it will work fine.

case2)if i write the method print(1) at LINE1
like this --> A.this.print(1)
then it also works fine

why the above code is behaving odd.
can any body pl. throw light on it
sunil

------------------
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunil,
could not find it in the documentation.
But it seems that you have to access methods of the outer class from the inner class through the <ClassName>.this.method() Syntax (You do not have to do that with member variables).
correct me if I am wrong.
Axel
I modified your code a little bit:
<code>
class A
{
void print() {System.out.println("print() outer");}
void print(int val) {System.out.println("print(int) outer");}
class B
{
void print() {System.out.println("print() inner"); }
void show()
{
print(); //same as this.print();
//print(1); // DOES NOT COMPILE
A.this.print(1); // this does W O R K
}
}
public static void main (String args[]) {
A.B inner = new A().new B();
inner.show();

}
}
</code>

[This message has been edited by Axel Janssen (edited June 07, 2001).]
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below code runs. Class B's constructor has access to Class A's print method.
<code>
public class A
{
void print() {}
void print(int val) {System.out.println("print()"); }
class B
{
void print(1);
void show()
{
print(1); //has access to the local print(1) not class
// A's print(1)
}
B() { print(1); //has access to class A's print(1) method
}
}
public static void main(String args[]) {
new A().new B();
}
}
</code>
 
sunil kathuria
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi axel and charlie
pl. read my problem again.
case1) if i donot define print() method in class B
and call both methods of A it will work fine.
case2)if i write the method print(1) at LINE1
like this --> A.this.print(1)
then it also works fine
(this means not only variables ,methods of outer class are also
accessible in inner class)
my question is- if i define print() in class B it only shadows
the outer class method but print(int val) is also available
in class B. why the compiler is giving error that the method
print(1) is not found in class B.

pl. help me
sunil
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunil,
You are calling Inner class's overloaded method, which is not actually defined in Inner class, and you are expecting Outer class's (similar) overloaded method should be called. It could't happen. Outer class and Inner class does't have Parent Child relation, both or independent, only Inner class has right to accesss Outer classes members, not the otherway round. Since Inner class has print() method, it looks for its overloaded method within Inner class and throws error if not found in Inner class.
Please clarify me if I am wrong.
Thanks & regards,
V. Srinivasan
[This message has been edited by V Srinivasan (edited June 08, 2001).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the JLS, the first thing that happens is that the compiler tries to find the method-name (not the entire method signature) within some class. Once it finds that method name, it then searches that class (and only that class) for the entire method signature. So by adding the print() method to B, you have made the print(int val) method non-accessible by using this.print(1).
An interesting aside: VisualCafe is unable to realize that the print(int val) method is non-accesible. If you type "this." it brings up a list of methods and "print(int)" is included in the list. A little bug.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic