aspose file tools*
The moose likes Beginning Java and the fly likes static and instance initialization blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static and instance initialization blocks" Watch "static and instance initialization blocks" New topic
Author

static and instance initialization blocks

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57


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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Dec 08, 2012
Posts: 47
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.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static and instance initialization blocks
 
Similar Threads
char
compilation error...
Local variable Initialization confusion
Help on this SCJP question
Method Variable