What type of access specifiers can we use for local variables?

Questions by naggeek

Showing Answers 1 - 24 of 24 Answers

vegetto

  • Jun 9th, 2008
 

hi there,
                 the type of access specifiers depend purely on how is the logic of your programme oriented:



CASE1:the best approach that is suggested by most of the top programmers have been to make your variables as private which is best for the purpose of security as well.

CASE2:suppose if you want that when you are instantiating your class then one particular method of that class should not have all the variables accessible to it but only few that you have declared in the beginning of the class then declare that method as well as the variable that you wnt to be accessible  as static and only that variable becomes accessible for that method.

CASE3:if you want that whatever happens you don't want any of a particular variable to change in any of the case then declare it FINAL.

  Was this answer useful?  Yes

Local Variables and Access Modifiers Can access modifiers be applied to local variables?

NO!

There is never a case where an access modifier can be applied to a local variable, so watch out for code like the following:

class Foo {
 void doStuff()
{
private int x = 7;
 this.doMore(x);
 }
}
 You can be certain that any local variable declared with an access modifier will not compile. In fact, there is only one modifier that can ever be applied to local variables—final.

Local variables (including formal parameters) are visible only in the method, constructor, or block in which they are declared. Access modifiers (private, public, ...) can not be used with local variables. All local variables are effectively private to the block in which they are declared. No part of the program outside of the method / block can see them. A special case is that local variables declared in the initializer part of a for statement have a scope of the for statement.

  Was this answer useful?  Yes

Sarje

  • Aug 18th, 2009
 

Local variable cannot be modified using any of the acceess specifiers means you can not write private or protected or public before them.

Local variables are by default (implicitlyprivate no need to make it explicitly private or protected or public otherwise it won't compile.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions