Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Class Declaration

 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're both right ;)

Apparently the syntax of a type declaration is (roughly) class_declaration | interface_declaration | ";".
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you , Friends
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic