Author Topic:   Inner Classes...
Saraswati
unregistered
posted April 28, 2000 02:22 PM           
I was going through a book which says "Members of inner class are known only within the scope of inner class and may not be used by outer class"

so,
Why is this code compiling???



class Outer
{
int outer_var=100;
void test()
{
Inner inner= new Inner();
inner.display();
}

class Inner {
void display()
{
System.out.println("Display outer_var:" + outer_var);
}
}
}

class InnerDemo{
public static void main(String[] args)
{
Outer outer=new Outer();
outer.test();
}
}


Here are we not calling the inner method in outer class?
Please correct me....

[This message has been edited by maha anna (edited April 28, 2000).]

satya5
ranch hand
posted April 28, 2000 02:29 PM             

"Member of inner class ...."

means member variables of the inner class can
be accessed only in the inner class, not the
outer class.

The code compiles because it accesses the outer
class variables in the inner class method.

This is allowed, with some restrictions ofcourse.

Regds.

- satya

maha anna
bartender
posted April 28, 2000 04:06 PM             
Saraswati,
Members of a innerclass means all the (var/methods/innerclasses/innerinterfaces) inside the inner class . This statement is applicable not only to an innerclass, it can also be applicable to any type of a class. For ex.

1. normal-packagelevel-toplevelclass
2. nested_top_level_class in other words static_innerclass
3. non-static_innerclass
4. local_innerclass_inside_a_method_OR_block_ofcode
5. anonymous inner class

Having said that, in your code you are not directly accessing the innerclass member (here the method display() ). What you are doing is accessing a very valid Outer class's ( its own ) member Inner . Once you have an instance of the Innerclass then you can always access the Innerclass's members through the created ref.

A class can always access its own members right?. If the owner can't access then its not fair right ? This is what happens in your code. So it compiles fine. Instead try the foll. code. This will not compile.


class Outer
{
int outer_var=100;
void test()
{
display();
Inner inner= new Inner();
inner.display();
}

class Inner {
void display()
{
System.out.println("Display outer_var:" + outer_var);
}
}
}

class InnerDemo{
public static void main(String[] args)
{
Outer outer=new Outer();
outer.test();
}
}


Satya,
What you say is correct. But that doesn't answer the qstn I think. What Saraswathi is asking is how can the outer class access the inner class method display().

regds
maha anna.

[This message has been edited by maha anna (edited April 28, 2000).]

satya5
ranch hand
posted April 29, 2000 11:30 AM             

Thanks for following up Maha...
hmm...I got the qstn wrong!
anyways...

- satya

maha anna
bartender
posted April 29, 2000 12:17 PM             
Satya,
Did I upset you. First I thought I needn't make a note to you. Then I thought Satya should know what he is actually thinking of the qstn like that. . Is it ok?

regds
maha anna

satya5
ranch hand
posted April 29, 2000 12:38 PM             

Perfect that you mentioned .... Maha!
NO, I am NOT upset ! see ... i am ...

I am glad you mentioned.
I would hate myself if I am
mis-guiding someone (even unintentionally).

Moreover, I am answering the qstn half-hearted
(sorry saraswathi ). actually I was hurrying
up for a meeting and then I though I will make
a quick pass at this issue.


Regds.

- satya

Saraswati
unregistered
posted April 29, 2000 01:48 PM           
Thanks Maha and satya.


So,By this can I say that I can also access the variables of Innerclass in the Outer?
Of Course with the instantiation of innerclass.

satya5
ranch hand
posted April 29, 2000 02:08 PM             

As Maha said :

"Members of inner class mean ....."

Also, I have seen many times people suggesting the
following:

"Write some code and check it ..."

Please donot get me worng. But if you follow the above
advice, it really helps. It really really increases confidence.

BTW, the answer to your question is YES.

Hope this helps.
Regds.

code:

class Outer1
{
int outer_var=100;
void test()
{
//display();
Inner1 inner= new Inner1();
inner.display();
System.out.println("Inner variable access = " +
inner.inner_var);

}
class Inner1 {

int inner_var = 5;

void display()
{
System.out.println("Display outer_var:" + outer_var);
}
}
}

class InnerDemo{
public static void main(String[] args)
{
Outer1 outer=new Outer1();
outer.test();
}
}


ps: I already had a class called Outer so I used Outer1.
I hope I am right this time.


- satya

|