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 variables in initialization blocks. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "variables in initialization blocks." Watch "variables in initialization blocks." New topic
Author

variables in initialization blocks.

Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 57
Hi,

Where would variables initialized in initialization blocks be used? Since the scope would be resolved to the block only, these variables cannot be used outside I believe.

Larsen
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Larsen Raja wrote:Hi,

Where would variables initialized in initialization blocks be used? Since the scope would be resolved to the block only, these variables cannot be used outside I believe.

Larsen

Hello Larsen, I recommend you write a test program and do some 'trial-and-error' coding it is a good way to resolve such doubts.

HtH

Ikpefua


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
John Jai
Rancher

Joined: May 31, 2011
Posts: 1372
Variables declared or initialized in instance block? variables declared within it's scope can be used for computations = normal to what you use local variables in a method.
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 57
I tried using such variables outside the block, say another method. But this brings up compilation error. The variable cannot be resolved outside the init block.


Larsen.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 2686

Which is exactly what you'd expect from any local variable - the scope is limited to the block they are declared in.
Dennis Deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 547
Ikpefua Jacob-Obinyan wrote:
Larsen Raja wrote:Hi,

Where would variables initialized in initialization blocks be used? Since the scope would be resolved to the block only, these variables cannot be used outside I believe.

Larsen

Hello Larsen, I recommend you write a test program and do some 'trial-and-error' coding it is a good way to resolve such doubts.

HtH

Ikpefua

Good to see you back again, Ikpefua


OCPJP 6
 
 
subject: variables in initialization blocks.
 
Threads others viewed
Blocks
pls help exams are on my head
Placement of static init block - Coding Conventions
final variables
What does a static block in class mean?
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture