It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why compile error??????/ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why compile error??????/" Watch "why compile error??????/" New topic
Author

why compile error??????/

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
At ////////////////1 i am getting compile error saying illegal start of variable.

Static method can have static variable .......then why it is error???

ublic class testin
{
static public int aMethod()
{
static int i=0;//////////////////////1
i++;
return i;
}
public static void main (String args[])
{
testin test = new testin();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Static method can have static variable .......then why it is error???


Local variables are not allowed to be static in Java. These variables are private to the method, it really doesn't make sense for these variables to be static.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jorge Restrepo
Greenhorn

Joined: Dec 24, 2005
Posts: 1
Since this is a class method you shoult use the class name rather than an instance:

testin.aMethod();
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Karthik,
You had posted similar kinda question here check this. and you had realized that local variables cannot be declared static.


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
santosh kothapalli
Greenhorn

Joined: Dec 27, 2005
Posts: 26
public class TimDig
{static int i=0;
static public int aMethod()
{
i++;
return i;
}
public static void main (String args[])
{
TimDig test = new TimDig();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}

if you replace ur line one to class variable it compiles fine.
instance variable cannot be static.


Santosh K<br />SCJP 1.4,SCWCD
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
method local variables can be declared as 'final' or
'static final'

BURN THIS IN UR MIND. there r no exception to this statement

niranjan


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
The ONLY modifier allowed in front of a local variable is final.
Niranjan, note the above. You cannot have "static final" for a local variable.
Sashi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why compile error??????/