File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes {} inside a method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "{} inside a method." Watch "{} inside a method." New topic

{} inside a method.

Meg Adal
Ranch Hand

Joined: Mar 12, 2002
Posts: 70
I wrote following program,it compiles and runs fine.But I was expecting compilation error for {}
I am not able to undestand why does Java allows to have {} inside a method?
Is it treated as Instance Initializer block?
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
it's perfectly legal to have a code block within a code block. i don't think it is commonly used, but the main effect is to limit the scope.
the code below shows an example of how it works:

use the [CODE] tags - it makes it much easier for people to help you.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Also, empty code blocks are perfectly legal, and occasionally useful (for instance, to act as placeholders for future code).

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Meg Adal
Ranch Hand

Joined: Mar 12, 2002
Posts: 70
Thanks All.
I agree. Here's the link:
subject: {} inside a method.
It's not a secret anymore!