Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access modifiers in a method

 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,

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.
 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic