wood burning stoves
The moose likes Beginning Java and the fly likes Effect of semi-colon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Effect of semi-colon " Watch "Effect of semi-colon " New topic

Effect of semi-colon

Nikhil Pancholi

Joined: Dec 10, 2004
Posts: 7
Hi all,
Just confused how semi-colon affects code compilation. For example the following:

interface I {

public class Inner {

Inner ( ) {
System .out . println ( "Inner Created" ) ;

compiles fine. I thought that because of two semi-colons after last two curly braces it won't. Why?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

A semicolon is a statement terminator. A lone semicolon is an empty statement. These are valid pretty much anywhere, including after blocks, as you've used them here. They're just ignored.

Originally, this was allowed to make the transition from C++ easier -- or so the legend goes.

[Jess in Action][AskingGoodQuestions]
Philip Heller
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
When the compiler sees

It interprets the close-curly as the end of the preceding block, just as if the semicolon weren't there. Then it interprets the semicolon as an empty statement, which is terminated by a semicolon just like non-empty statements.

Here's an extreme example. A source file could look like this:

The compiler consider this to be ten empty statements. Not a very useful source file, except that it teaches us something about semicolons. Hmmm, so maybe it is useful after all.

Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Nikhil Pancholi

Joined: Dec 10, 2004
Posts: 7
Yes it indeed was useful . Thanks for clearing my doubt.
It is sorta covered in the JavaRanch Style Guide.
subject: Effect of semi-colon
It's not a secret anymore!