Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assignment operator

 
jaysingh solanki
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
class Test
{
int i=4;
i+=5;//line 1
public Test()
{
System.out.println(i);
}
public static void main(String arg[])
{
Test t=new Test();

}
}
My question is why line one gives compile time error
identifier exp.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"i" is an instance variable (since you don't see the keyword static anywhere).
Consequently, trying to initialize it at the static (class) level will result in an error. If you are trying to make "i" a class-level variable, use the static keyword and place their initiailization in a static { ... } block. Otherwise, "i" is an instance variables and need to be initialized in a constructor, not at Line 1 as it is now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic