This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static variables inside a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variables inside a static method" Watch "static variables inside a static method" New topic
Author

static variables inside a static method

kavundamani senthil
Greenhorn

Joined: Feb 28, 2008
Posts: 16
when i try to declare a static variable inside a static method it shows an error as illegal start of expression. is it legal to do so

public class Test {
static int zee=5;
public static int amethod(){
static int zoo=7;
int x =1;
return x;
}
public static void main(String argv[]) {
System.out.println("I love Zamples and J2SE 6!");
int y= amethod();
System.out.println(y);
Test z = new Test();
System.out.println(zee);
}

Test.java:7: illegal start of expression static int zoo=7; ^ 1 error
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Unlike C and C++, this is not legal in Java. Instead make it a class or instance variable.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You can't write "public" "private" or "static" inside a method.
kavundamani senthil
Greenhorn

Joined: Feb 28, 2008
Posts: 16
thanks got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables inside a static method