Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg. local variables

 
Angela Narain
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MyClass {
static int x=10;

public static void main(String[] arg){
static int x=20;
System.out.println("Value of x :"+ x);
}
}
Why does the above code not compile
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
My best guess is that you cannot declare your "x" to be a static in the main method.
Possible reason... static variables are class variables and I don't think they can be declared within methods (including main()).
Do correct me if I am wrong.
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 08, 2001).]
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right.
Inside an instance method a static variable makes no sense. Wihtin a static one would be redundant (I guess)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic