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 Static variable 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 "Static variable" Watch "Static variable" New topic

Static variable

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Hello Friends,

I have a confusion here .This is from EFS mock test.

public class Test2{

public int aMethod() {
static int i = 0;
return i;
public static void main (String args[]) {
Test2 test = new Test2();
int j = test.aMethod();

wy does this not compile..! It says illegal start of expression on static keyword..! I tried making the method static still it complains..!

Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

" static int i = 0"

you are trying to declare a static variable within a method. This is not legal. static variables are class variables only. Declared outside of methods.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
just to add the only modifier you could use for local variable (declared inside method including parameters) is
final. no other modifier is allowed.
[ September 02, 2005: Message edited by: Santana Iyer ]
I agree. Here's the link:
subject: Static variable
It's not a secret anymore!