File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes access modifiers in a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "access modifiers in a method" Watch "access modifiers in a method" New topic

access modifiers in a method

Ken Sampson
Ranch Hand

Joined: Sep 26, 2007
Posts: 45

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
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
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...



SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Ken Sampson
Ranch Hand

Joined: Sep 26, 2007
Posts: 45
Thanks a lot guys!!!
I agree. Here's the link:
subject: access modifiers in a method
It's not a secret anymore!