• 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

Doubt about method local class

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Outer
{public void method()
{ int v =34;// if v is final
class MethodLocal
{{System.out.println("MethodLocal class calling local variable v ="+ v);}
}
new MethodLocal();
}
public static void main(String [] args)
{
new Outer().method();
}
}

Why it gives output only when variable is final

if v is final out put is

MethodLocal class calling local variable v = 34

if v not final i know the reason but can anybody explain here what "final"
signifies and why it only accesses final variables
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below is stated in page 6 of the Java Inner class Specification document.
>>>>>>>>>>>>>>>>>>>>
Note the final declaration. Local final variables such as array are a new
feature in 1.1. In fact, if a local variable or parameter in one class is referred to
by another (inner) class, it must be declared final. Because of potential
synchronization problems, there is by design no way for two objects to share
access to a changeable local variable
>>>>>>>>>>>>>>>>>>>>>>>

To underdstand what it means, read this link
http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-java101_p.html

Happy Learning!
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
google Javaranch and you will find a lots of threads. Here is one.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just posting the words of one of the great senior rancher


You see, the access that inner classes have to local (method) variables and parameters is an illusion. You don't have any access to local variables. What happens behind the scenes is that the Java compiler creates instance variables in your inner class which contain copies of the local variables you "access".

The problem is, if either the method or the inner class were allowed to change its copy of the variable, the different copies would get out of sync and the illusion would be shattered. This is why the compiler forces you to declare the variable final: so that it can freely generate multiple copies and create the illusion that your inner class has access to them.

The inner class implementation is full of such trickery. It had to be, because Sun added them to the Java language without changing the JVM spec. For example, the special access that inner (and nested) classes have to private variables of their enclosing class is... you guessed it... an illusion. Access is granted through special accessor methods generated by the compiler.

If you don't believe me, use javap to disassemble some class files that have inner classes

 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic