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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 42
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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static and instance initialization blocks
 
Similar Threads
Local variable Initialization confusion
Method Variable
Help on this SCJP question
compilation error...
char