aspose file tools*
The moose likes Beginning Java and the fly likes Why private, public variables are not allowed inside a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why private, public variables are not allowed inside a method" Watch "Why private, public variables are not allowed inside a method" New topic
Author

Why private, public variables are not allowed inside a method

Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
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

Joined: Apr 25, 2005
Posts: 59
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

I have a return question for you: Why would you want to make a local variable public, private or protected - what would it mean?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
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

Joined: Oct 07, 2005
Posts: 637
Clarifying doubts is perfectly alright, but it would help trying to get the big picture before bursting forth with a question.

Think about it.


ASCII silly question, Get a silly ANSI.
Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 804

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 ]

Regards Pete
Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
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

Joined: Oct 21, 2004
Posts: 804

I want to discuss every aspect of the problems

Is this an assignment?
[ November 21, 2005: Message edited by: Peter Rooke ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why private, public variables are not allowed inside a method