This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt in Class Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Class Declaration" Watch "Doubt in Class Declaration" New topic
Author

Doubt in Class Declaration

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50

Hi Friends,

I thought the Class Declaration is like



the Following code also compiles and executed fine.



is the Syntax of Class Declaration with semicolon acceptable, shall i get Explanation on this.

Thanks


N.Senthil Kumar
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

As you noticed, the semi-colon is allowed but not necessary. I don't know why this is, maybe it was done to make life easier for C++ programmers switching to Java; in C++, the rules for semi-colons are slightly different.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's not really a "class declaration with a semicolon", it's a "class declaration followed by an empty statement". Statements end with a semicolon, and statements can be empty strings.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

But you can't put arbitrary statements outside a class declaration - it would be strange if you could put an empty statement outside a class declaration.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

We're both right ;)

Apparently the syntax of a type declaration is (roughly) class_declaration | interface_declaration | ";".
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Thank you , Friends
 
wood burning stoves
 
subject: Doubt in Class Declaration