• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables inside a static method

 
kavundamani senthil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike C and C++, this is not legal in Java. Instead make it a class or instance variable.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't write "public" "private" or "static" inside a method.
 
kavundamani senthil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic