• 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

innerclass

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Im not clear on why the innerclass defined inside a method can access only variable defined final in the outerclass. Would appreciate ur help if some one can throw more light on this!!
------------------
Thanks,
Senthil
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Senthil,
ur question is really interesting!!
To make the concept clear, I have created one example. Pls go through this..

In general, A member method in a class can access member variables defined in its body. It holds good for inner class also.in addition to that, Inner classes whose declarations do not occur in a static context may freely refer to the instance variables of their enclosing class. The only problem here is the local variables and member method parameters.
Because the class is inside the method, it can see the local variables in the enclosing method. But it is not physically existing inside the class. so inner class methods can't refer/access these local variables. to tackle this, the compiler creates a hidden copy of the 'final' local variable that the inner class can access safely.
the example illustrated above will give u an output of

inside local var :20
inside local class,free access to enclosing class var :0

hope this would be clear..
Others, pls correct me if I am wrong!!
------
Thanks,
S.Baskaran.


[This message has been edited by Baskaran Subramani (edited September 05, 2000).]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
Take a look at this example code
public class Outer
{
final int i = 10;
int j=5;
public void callme()
{
int k=9;
class Inner
{
public void innercalls()
{
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("k="+k);
}
}
Inner i = new Inner();
i.innercalls();
}
public static void main(String[] argv)
{
Outer o = new Outer();
o.callme();
}
}
If you try and compile this code, you will get a compilation error. The error is not for j which is not defined as final in the outer class but for k which is NOT defined as a final in the method. This means that an inner class which is defined within a method has access to all the variables defined in the outer class but to have access to variables defined within the method these variables have to be defined as final.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
really...........fantastic and great examples you have given friends.....i am very much impressed with ur examples....they are crystal clear....keep it up!
surya teja
------------------
 
senthilkumar74
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation. In convinced.

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE has also given a good explanation about accessing variables from an inner class.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic