Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method local inner class

 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method local inner classes are just like method members . so it may be final . because it is class so it may be abstract . but why this class can't access methods members .

thanks in advance .
bye , pooja
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class defined within a method can access local method variables only if they're final. Otherwise, the inner class object might outlive the local method variables which only exist during the scope of the method and are typically destroyed when the method exits. But if the local variables are final, then the compiler can copy their values into the inner class object.
 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final variable are also destroyed after method termination ( i think ) .
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pooja jain:
final variable are also destroyed after method termination ( i think ) .

Yes, but by then they've also been "inlined" as part of the locally defined object. This could not happen if the variable wasn't final.
 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc , please explain it in simple word . what a final variable can do that non final can't do . if you have any anology then it would be more batter .
thanks , pooja
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables don't do anything. Variables are the nouns, methods are the verbs. A final variable is one that cannot be changed unless it's uninitialized when declared, in which case it must be initialized in a constructor or initialization block.

I hope this helps...
 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff you got the wrong or my question was not clear . I meant was what a final variable can do for method local inner class that non final variable can't do .
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the meaning of this final variable can be copied in object .
please help . thanks .
pooja .
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of a final variable cannot change, so that value (not the variable) can be integrated into the object as a "constant."

In the example below, x is a local variable that currently holds the value 1. But x is not final, so that value might change later. If a LocalClass object is created, that object might continue to exist after the method has exited. And so if LocalClass had been allowed to use x, then there would be a problem, because x would no longer exist and the object would have no way of knowing whether the value 1 is still valid.

On the other hand, y is also a local variable. But y is final, so its value of 2 cannot change. Now if a LocalClass object is created, it can simply copy the value of y. When the method exits and y is destroyed, that's okay because the object doesn't have to go back to y for its value. The object uses a "hard-coded" value of 2 instead of the variable y.

(With objects, of course, we're talking about references being final. So if refA is final, then refA will always "point" to the same object.)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic