This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why private, public variables are not allowed inside a method  RSS feed

 
Hemant Agarwal
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?
 
Frank Ertl
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • 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
 
Stuart Ash
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.
 
Peter Rooke
Ranch Hand
Posts: 860
1
Java Linux Spring
  • 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
Ranch Hand
Posts: 860
1
Java Linux Spring
  • 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 ]
 
Nikhil Arora
Greenhorn
Posts: 18
Eclipse IDE Java MySQL Database
  • 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?
 
Junilu Lacar
Sheriff
Posts: 10949
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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: 10949
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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: 18
Eclipse IDE Java MySQL Database
  • 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: 10949
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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: 18
Eclipse IDE Java MySQL Database
  • 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: 10949
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Arora wrote:I get it now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!