• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static and instance initialization blocks

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does line 10 throw a compiler error
whereas line 15 does not?

As per me if line 10 throws an error due to the
static issue , its the same case for line 15


Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:
line 10 throws an error due to the
static issue , its the same case for line 15

no, they are not same case . initializer tutorial (<-- click)
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 15 will assign 17 to the static variable x every time a new instance of the class is created. While kind of an odd thing to want to do, it is a logical possibility.
Line 10 does not make sense, the static initializer runs when the class is loaded into memory, an instance does not have to be created and y is an instance variable.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic