• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing Interface Variables ?  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Given the code below, i cannot figure out why Class B compiles fine, whereas Class A doesn't...
the compiler says "Syntax error on token ";",,expected".

Any idea?
Thank you!
Dave

 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside the class (and outside of any methods or initialization blocks) you can only have variable declarations. You cannot initialize a variable in a separate statement. Initializing with declaration is allowed as after all it is a declaration. So if you want to initialize i in class A, then either initialize it in the declaration or in a initializer block...
 
Davide Crudo
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!

Dave
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well, if you want the code to run then as Ankit said either Line 1 or Line 2 are the valid ways of initializing the variable i. In case of static variables you could use the static initialization block instead of instance initialization block.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!