permaculture playing cards*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static variable" Watch "Static variable" New topic
Author

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;
i++;
return i;
}
public static void main (String args[]) {
Test2 test = new Test2();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

" 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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
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: http://aspose.com/file-tools
 
subject: Static variable
 
Similar Threads
Doubt in Static Method Access.
why cant the output of the code is 2
why compile error??????/
About the static
method