• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Why private, public variables are not allowed inside a method

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why private, public variables are not allowed inside a method

public class Experiment
{
public void exper ()
{
private int i = 2;
}
}

When i try to compile this code this compile time error is coming

javac Experiment.java
Experiment.java:5: illegal start of expression
private int i = 2;
^
1 error

What is this error? Why it is coming?
I have tried to make i as public, protected but the same error is coming
Why?

Only default modifier is working?
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables declared inside a block (i.e. code between two curly braces) are only visible inside this block, so there is no sense in declaring them private, public or protected.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a return question for you: Why would you want to make a local variable public, private or protected - what would it mean?
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just by mistake I have done that and I got the error and I want to clear my doubts. In the same way static modifier is not allowed inside a block of java but in VB static variables can be defined inside a method
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarifying doubts is perfectly alright, but it would help trying to get the big picture before bursting forth with a question.

Think about it.
 
Bartender
Posts: 1049
19
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why would you want to make a local variable public, private or protected - what would it mean?


For member variables - Information hiding
but not local variables.

Also if you are using local variable you might want to minimise their scope. If you are using a method argument and you know it's not going to change then declare it final.
[ November 21, 2005: Message edited by: Peter Rooke ]
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter, Stuart for your suggestions.
Anyone want to comment on this topics are welcome.
I want to discuss every aspect of the problems
 
Peter Rooke
Bartender
Posts: 1049
19
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to discuss every aspect of the problems


Is this an assignment?
[ November 21, 2005: Message edited by: Peter Rooke ]
 
Greenhorn
Posts: 19
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I do not want my variables' values to be visible to anyone outside the method?
I know questions like "Why I would like to do that would be asked" but I just want to know the need of eradicating the use of these keywords and making them illegal. I am clear on the variable scope.



What if I do not want doStuff to access my variable's value?
 
Sheriff
Posts: 14750
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikhil Arora wrote:I just want to know the need of eradicating the use of these keywords and making them illegal


What is the point of allowing them in that context if they don't make any sense at all? Programming is very much like math. It's logical and precise. In either case, if it doesn't add up, then it can't be done.
 
Junilu Lacar
Sheriff
Posts: 14750
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikhil Arora wrote:What if I do not want doStuff to access my variable's value?


And just like in programming, you have to be precise. Exactly what do you mean when you refer to "my variable's value"? What variable are you referring to here? Point out the exact line number and the name you used.
 
Nikhil Arora
Greenhorn
Posts: 19
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Nikhil Arora wrote:What if I do not want doStuff to access my variable's value?


And just like in programming, you have to be precise. Exactly what do you mean when you refer to "my variable's value"? What variable are you referring to here? Point out the exact line number and the name you used.



Line number is 3 and the variable name is x.
 
Junilu Lacar
Sheriff
Posts: 14750
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote:

Nikhil Arora wrote:What if I do not want doStuff to access my variable's value?
...
Line number is 3 and the variable name is x.


Your question is very illogical. You said you understand scoping. Then you ask what if you don't want doStuff() to access variable x.  Then why declare x inside doStuff() if you don't want doStuff to access it?  Declaring it private is illegal but let's just say it wasn't. How is that even logical, again, given that x is inside the doStuff() body? What does "private" even mean in this context then? Of what use is a variable if you can't access it? Honestly, this is an absolutely nonsensical scenario you are proposing and an even more nonsensical question.
 
Nikhil Arora
Greenhorn
Posts: 19
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You wrote:

Nikhil Arora wrote:What if I do not want doStuff to access my variable's value?
...
Line number is 3 and the variable name is x.


Your question is very illogical. You said you understand scoping. Then you ask what if you don't want doStuff() to access variable x.  Then why declare x inside doStuff() if you don't want doStuff to access it?  Declaring it private is illegal but let's just say it wasn't. How is that even logical, again, given that x is inside the doStuff() body? What does "private" even mean in this context then? Of what use is a variable if you can't access it? Honestly, this is an absolutely nonsensical scenario you are proposing and an even more nonsensical question.



Junilu, thanks for your sincere effort. I get it though but sometimes you need to confirm your insights and findings from someone who is better than you so that you can be sure that you are thinking in the right direction. Thanks for being that someone. I get it now
 
Junilu Lacar
Sheriff
Posts: 14750
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikhil Arora wrote:I get it now

 
A feeble attempt to tell you about our stuff that makes us money
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!