This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static variables inside a static method  RSS feed

 
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
Posts: 20893
81
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
Posts: 53742
127
  • 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
Boost this thread!