Don't understand Inner Classes and how to use it
Hi
As you guess i 'am a newbie!
I don't understand Inner Classes, particulary members(methods & fields) that an Inner method is able to manipulate.
So I know that the methods of an Inner class (respectively Outer Class) instance can access members (private or public) of an instance of the Outer Class (respectively Inner Class).
I tried to answer to a quizz : http://java.sun.com/developer/onlineTraining/new2java/supplements/quizzes/January03.html
In the following class definition, which variables are inaccessible within the method of the inner class?
class Test1{
publicstaticint a = 1;
privatestaticint b = 2;
publicint c = 3;
privateint d = 4;
publicstaticclass Inner{
int e = 5;
publicvoid aMethod(int f){
int g = 6;
// What can't be accessed here?
}
}
}
A. b, c, d
B. c, d
C. b, c, d, f
D. None of them
In my opinion members (public or private) of the Outer Class can be accessed by methods of the Inner Class; e.g:a, b, c,dthus for me the answer is D.
Obviously i'm wrong, but why?
[1977 byte] By [
nextOnea] at [2007-10-1 0:51:23]

> Hi
>
> As you guess i 'am a newbie!
> I don't understand Inner Classes, particulary
> members(methods & fields) that an Inner method is
> able to manipulate.
>
> So I know that the methods of an Inner class
> (respectively Outer Class) instance can access
> members (private or public) of an instance of the
> Outer Class (respectively Inner Class).
>
> I tried to answer to a quizz :
> http://java.sun.com/developer/onlineTraining/new2java/
> supplements/quizzes/January03.html
>
> In the following class definition, which variables
> are inaccessible within the method of the inner
> class?
>
> class Test1 {
>public static int a = 1;
>private static int b = 2;
>public int c = 3;
>private int d = 4;
>public static class Inner {
>int e = 5;
>public void aMethod(int f) {
>int g = 6;
>// What can't be accessed here?
>}
>}
> }
>
> A. b, c, d
> B. c, d
> C. b, c, d, f
> D. None of them
>
> In my opinion members (public or private) of the
> Outer Class can be accessed by methods of the Inner
> Class; e.g:a, b, c,dthus for me the answer is
> D.
> Obviously i'm wrong, but why?
Inner class method can access all the private members of the class. But the inner class is static. So the non-static members cannot be accessed directly (i.e. c and d).
***Annie***
Thank you so much Annie!Is that a general rule ? methods of static classes can only access to static members of the caller intance (except private static members). Am i right?
> Is that a general rule ? methods of static classes
> can only access to static members of the caller
> intance (except private static members). Am i
> right?
Yup. It comes from the fact that static methods cannot access non-static members or methods directly. The same applies for this one too.
***Annie***
Thank you once again Annie
But what about static classes with non-static methods :
import static java.lang.System.*;
import java.io.*;
/*[b]static[/b]*/ class A{ // STATIC CLASS; BUT ERROR AT COMPILE TIME
public A(String x, String y)
{ this.x = x ; this.y = y;}
public void edit() { out.println ("x and y :" +this.x +" "+this.y);} // NON STATIC METHOD
private String x;
private String y;
}
public class B { // NON STATIC CLASS
public static void main (String args[]) { // STATIC METHOD
A a = new A("Hello", "World");
a.edit();
}
}
Here main is static method of a non static class; and it's accesses to a non-static method (edit) of an "static" (?) class A.
[kong@localhost dev]$ javac B.java
B.java:4: modifier static not allowed here
static class A{ // STATIC CLASS
^
1 error
Why this error?
> Is that a general rule ? methods of static classes> can only access to static members of the caller> intance (except private static members). Except for the "Except" part :) inner classes can access privatemembers of their outer classes.
http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.htmlRead this article... it will clear your doubts.Anyway, why are you using import static java.lang.System.*;?***Annie***
Thanks Annie ...to use out.printn instead of System.out.printnNow with jdk 1.5 we could use sqrt() instead of Math.sqrt() so you have to import the package with static word
> Thanks Annie ...
>
> to use out.printn instead of System.out.printn
>
> Now with jdk 1.5 we could use sqrt() instead of
> Math.sqrt() so you have to import the package with
> static word
OK thanks... I still haven't looked at 1.5
***Annie***