File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes accessability of outerclass members in inner class !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "accessability of outerclass members in inner class !!!" Watch "accessability of outerclass members in inner class !!!" New topic
Author

accessability of outerclass members in inner class !!!

sunil kathuria
Greenhorn

Joined: Jun 06, 2001
Posts: 24
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

------------------
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
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).]
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
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

Joined: Jun 06, 2001
Posts: 24
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
V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
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).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
 
subject: accessability of outerclass members in inner class !!!
 
Similar Threads
Nested Classes
Outer-Inner Question
innerclass
literals
inner class doubt