• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Illegal modifier for parameter y, only final is permitted

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting error while declaring fields inside the main method......please tell me what is wrong in this

error:-
Illegal modifier for parameter y, only final is permitted


IfClause.java :-



Screenshot is attached also for your reference
Filename: error.bmp
Description: Error
File size: 648 Kbytes
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot set visibility scopes (private,...) to local variables.
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You cannot set visibility scopes (private,...) to local variables.

as said by Christophe Verré
but why, may i know the reason behind this ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it make sense ? What benefits would you get by making it private ? Private to whom ? The Local variables scope is already well defined : within the scope of the method it lives in.
 
Sheriff
Posts: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static also doesn't apply to local variables, unlike in C / C++.
 
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Static also doesn't apply to local variables, unlike in C / C++.



I actually like this feature in C/C++. Granted it is not really a local variable. It's more like a global variable that is private to the function.... or maybe it's more like a local variable, whose scope doesn't end when the function returns.

Henry
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Would it make sense ? What benefits would you get by making it private ? Private to whom ? The Local variables scope is already well defined : within the scope of the method it lives in.

as said by Christophe Verré

Thanks fro your response, i got it what you are saying.

 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. Local variables are local. Meaning you can't access them through a global scope.
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i want to know that if there is method say abc() which is public for instance like :-



[b]

From another class, i have created the object of this class and accessed this abc() method...success..
But why i'm not able to access the field x inside this method ?
I can access any field i.e. global variables only but why can't for local variables ? how can i put values in this local field through the object created ?
please tell me this briefly...
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote:Now i want to know that if there is method say abc() which is public for instance like :-





From another class, i have created the object of this class and accessed this abc() method...success..
But why i'm not able to access the field x inside this method ?


To put it in a simple way: because when the method call returns, the variable no longer exists.

Vinod Vijay wrote:I can access any field i.e. global variables only but why can't for local variables ? how can i put values in this local field through the object created ?
please tell me this briefly...


A local variable is NOT a field. Have you ever heard of parameters?
 
What's that smell? I think this tiny ad may have stepped in something.
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic