File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. local variables" Watch "Reg. local variables" New topic

Reg. local variables

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
[This message has been edited by Shyamsundar Gururaj (edited September 08, 2001).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You are right.
Inside an instance method a static variable makes no sense. Wihtin a static one would be redundant (I guess)

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Reg. local variables
It's not a secret anymore!