File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initializer block 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initializer block" Watch "initializer block" New topic
Author

initializer block

Udit Manektala
Ranch Hand

Joined: Sep 17, 2006
Posts: 30
There's this problem that confuses me about intializer blocks.

Why is this code not wrong\giving me an error. After all, it is redeclaring the variable width, right ?

I came across this in the questions of the Mughal Rasmussen book and tried it myself and it works. I just need to know Why??... it doesnt make any sense to me.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Originally posted by Udit Manektala:
There's this problem that confuses me about intializer blocks.

Why is this code not wrong\giving me an error. After all, it is redeclaring the variable width, right ?

I came across this in the questions of the Mughal Rasmussen book and tried it myself and it works. I just need to know Why??... it doesnt make any sense to me.


The instance initializer is declaring a local variable named "width". This variable is assigned the value of 15, not used afterwards, and goes out of scope when the initializer completes.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initializer block
 
Similar Threads
forward reference doubt
Overloaded method
NoSuchMethodError
Beginner Question: Class Capabilities
question on initialization