aspose file tools*
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
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: 19670
    
  18

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: 38363
    
  23
You can't write "public" "private" or "static" inside a method.
kavundamani senthil
Greenhorn

Joined: Feb 28, 2008
Posts: 16
thanks got it
 
Don't get me started about those stupid light bulbs.
 
subject: static variables inside a static method