Java certification Question
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jayakanthan_muthulingam
Posted On:   Saturday, August 7, 2004 12:56 AM

I am compiling the following code
------------------------
public class Ex1
{
public int aMethod()
{
static int i=0;
i++;
return i;
}

public static void main(String args[])
{
Ex1 ex=new Ex1();
ex.aMethod();
int j=ex.aMethod();
System.out.println(j);
}
}
----------------
It is not compilimg and says illegal start of experssion at the line where i decalred static int i=0; but the answer says in testking answer is 2. all dumps says the same answer
please help me

Re: Java certification Question

Posted By:   sujata_samal  
Posted On:   Monday, September 20, 2004 10:11 PM

Java does not support local static variables.
U can move to the following link for further details.


http://forum.java.sun.com/thread.jsp?thread=549662&forum=31&message=2699088




Thnx
Sujata

Re: Java certification Question

Posted By:   sujata_samal  
Posted On:   Monday, September 20, 2004 10:09 PM

Java does not support local static variables.
U can see the link for further details.



Thnx
Sujata

Re: Java certification Question

Posted By:   amit_vyas  
Posted On:   Tuesday, August 24, 2004 09:20 PM

problem is in static int i = 0; because u cant decalre and calss level visibilty variable in local visibility method;put that line out side the method make variable class level and ur code will work

Re: Java certification Question

Posted By:   neal_ravindran  
Posted On:   Sunday, August 8, 2004 06:48 AM

The rules of java language states that a static variable cannot be declared inside a non-static method. The java compiler assumes that you are a nut case when it sees a static variable declared in a non-static method, as it (the compiler) has been manufactured with the notion that static variables can only belong to a class or to a static method and not to a non-static method.



I have not compiled this, but if you change the non-static method to a static one, it should work fine.

About | Sitemap | Contact