Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why private, public variables are not allowed inside a method

 
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
Saloon Keeper
Posts: 15207
36
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: 847
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: 847
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic