• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can resolve symbol ???

 
Mateen Nasir
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have compiled the following code and receive the error given below

class ATest {
static int i = 2 ;
static int [] b = new int[i] ;
i = 2039;
public static void main( String[] args ) {
System.out.println ( " testing" ) ;
// 2
}
}

MESSSAGE FROM COMPILER

ATest.java:4: <identifier> expected
i = 2039;
^
ATest.java:4: cannot resolve symbol
symbol : class i
location: class ATest
i = 2039;
^
2 errors

I FAIL to understand why the compiler cannot resolve the variablr that i have jsut defined above using the variable inside the method i.e. coping thesame statement that gives the error to location 2 inside main does not give any error
I cannot understand why??
can anyone help
 
Rehan Malik
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this is because you cannot assign another value to the variable OUTSIDE of a method.
It works within the method but not outside.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mateen
After a variable is declared the only place you can assign a new value is within a block of code.
Within a static initializer block (for static variables), an instance initializer block (for instance variables) or in a constructor of method.
like this:

hope that helps
Dave
SCJP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic