I have learnt that in a method only final modifier is permitted. Could you please tell me why other modifiers such as private, public etc. are not permitted and why final is permitted?
Ken Sampson
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
posted
0
If you assign private to a variable, it is accesible to thta method only, no other method in class can use it. i think you can assign public access modifier to variable is ok. Correct me if i am wrong
Thanks<br />Dinesh
Vishal Matere
Ranch Hand
Joined: Jan 22, 2008
Posts: 81
posted
0
Because, LOCAL variable that are defined inside method are specific to that method only. These variables do not define the STATE of the objects as do instance variables do.
Local variables have visibility only inside that method/block and hence other modifier like Public/private are now appropriate here.
And you know how JAVA is, Anything that meaningless is not PART of java...
HTH
V
SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Ernest Friedman-Hill
author and iconoclast
Marshal
Local variables can't be private or public because those terms are meaningless; there's not even a syntax for accessing a local variable outside of the method that defines it. Since there's no access, there's no reason for access control.
"final" is meaningful though; it makes just as much sense to have a non-changeable local variable as a non-changeable member, and therefore, it's implemented.