-5

In this program Is it possible to use the access specifier inside the method

class AccessTest{
int i;

   public static void main (String... str) 
  {
   int i;
   private int a = 1;
   protected int b = 1;
   public int c = 1;
   System.out.print (a+b+c);
  }

}

what is the final output can anybody explain this ?

Vignesh Pichamani
  • 7,950
  • 22
  • 77
  • 115
Shivam
  • 674
  • 1
  • 4
  • 25
  • 6
    To begin with, why would you want `private`, `protected` or `public` variables inside a method? What would be the meaning of having this? – Luiggi Mendoza Sep 03 '13 at 06:36
  • 2
    "what is the final output can anybody explain this ?" just run it and see for yourself – Marco Forberg Sep 03 '13 at 06:36
  • "What is the final output".. what did it output when you tried it? – Nanne Sep 03 '13 at 06:37
  • Have you tried to compile this? It does not even compile. What do you expect to get as result? – Ean V Sep 03 '13 at 06:38
  • If I used the public only at the place of the private or protected then also I have got the error ... – Shivam Sep 03 '13 at 06:39
  • 1.It might be giving error "Illegal modifier for parameter a/b/c; only final is permitted" 2.y u want to use access specifier inside the method – Shakeeb Ayaz Sep 03 '13 at 06:39

5 Answers5

2

The access modifiers specify the visibility of one class' fields for other classes. Since local variables (those declared inside methods) are never exposed, it doesn't make sense to set a certain access for them. Actually it's a compilation error, if you try it.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
1

No it is not possible. As there are no use of it so it is restricted.

Local veriable's scope are restricted with in body so there would be no use of modying there access.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

You can't use private, protected, public modifiers inside a method. Final output is compilation error.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

You cannot set visibility scopes (private,...) to local variables. because The Local variables scope is already well defined : within the scope of the method it lives in.

bNd
  • 7,512
  • 7
  • 39
  • 72
0

Variables declared in a method are local to the method; i.e. they can't be accessed outside the method.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39