• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

assignment operator

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic