Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Yikes! Help with Inner Classes

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you explain why I cannot access an inner classes method where the inner class is defined inside a method? Why is it that the inner classes method can refer to a method in the outer class?
When an inner class is defined under the outer class, I can access the inner classes method from a method of the outer class,
but when the inner class is defined in a method I cannot acess it
from an outer classes method?
Thank you.
Below is the code.
<Code>
class First {
public static void main(String [] args) {
First f1 = new First();
f1.method();
}
public void method() {
methA().new Second().sayGoodnightGracy(); // **This line does
// **not work
}
public void methA() {
class Second extends First {
void sayGoodnightGracy() {
System.out.println("Goodnight, Gracy");
}
}
}
}
</CODE>
 
Sheriff
Posts: 16669
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same reason you can't access one method's local variables from another method. A class defined inside a method has the same scope as other local variables defined in that method. Classes defined inside a method are usually anonymous. If you have a named class inside a method, new instances of that class can only be made inside the enclosing method.
There are other rules that apply to method-local scoped classes but they aren't directly related to your question.
HTH,
Junilu
[This message has been edited by JUNILU LACAR (edited June 02, 2001).]
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I understand what you said about not being able to access a methods local variables, but why is it then that I can have an inner class, and still access it's inner variables; that is assuming that the inner class has an outer class and the inner class is not contained in a method.
It would seem given what you said about scope that I would have access to the inner class, but not it's methods.
I really missing something, please explain.
 
Junilu Lacar
Sheriff
Posts: 16669
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Charlie Swanson:
It would seem given what you said about scope that I would have access to the inner class, but not it's methods.


OK, take the following:
<pre>
class Outer
{
void methodA()
{
// you don't expect to be able to access
// objectB from here do you?
System.out.println(objectB.toString()); // Illegal!
}
void methodB()
{
Object objectB = new Object();
System.out.println(objectB.toString());
}
void methodC()
{
// well, same thing goes here: you shouldn't
// expect to be able to access foo1 from here either
foo1.bar(); // Illegal!
}
void methodD()
{
class Foo
{
void bar()
{
System.out.println("Foo.bar()");
}
}
Foo foo1 = new Foo();
foo1.bar();
}
}
</pre>
In the above code, only methodD knows about the existence of class Foo so naturally, nothing outside of methodD will be able to access class Foo.
Junilu

[This message has been edited by JUNILU LACAR (edited June 02, 2001).]
 
Junilu Lacar
Sheriff
Posts: 16669
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I think I got your question now. Sorry about that.
This is just how I view it though--others may be able to explain it better:
The outer class is given special privileges to any inner classes it directly contains, i.e. those that are defined outside its methods but inside the class. I guess it kind of makes sense to do that because in essence, the outer class "owns" the inner class. In the case of classes defined inside methods, think of the direct "owner" as the method, not the class, so only the method has given special privileges.
FWIW, I used RHE to review and they have a pretty good discussion of the two types of inner classes.
Junilu
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the above explaination. I do understand what you said there.
In the example below, I applied the concept of scope to a class; but, it did not apply. Notice how the inner method, "innerfoo1" was able to be created. The only change in my original question was the method.
The clearing up of the above is appreciated.
<code>
class yfoo1 {
class yrafoo1 {
void innerfoo1() { System.out.println("yrafoo1"); }
}
public static void main(String args []) {
new yfoo1().new yrafoo1().innerfoo1();
}
}
</code>
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu,
Thank you very much for the help.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic