Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

!!!MOST UNBELIVEABLE!!!

 
Amit Roy
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear frns,
examine the code below carefully and just tell wethet it will compile.

yes the code above will compile !!Zapped !!!
YES A FULLY EMPTY FILE IS A VALID SOURCE FILE AND COMPILES WITHOUT ANY ERROR
can any one give me a logical explanation?
also
A Java file without any class or interface definitions can also be compiled.
AMIT
 
Ramesh Kadur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit,
See whether you get any class file when you compile an empty file. You don't get any. That means there is nothing to compile. To answer your second part, in Java, everything has to be in a class. You can not have free standing methods or variables as in C. If you have some variable or method not inside any class, then compiler issues this error message:
Class or interface declaration expected
Ramesh.
 
Bob Lulyk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Amit:
The Java Language Specification basically says that a Java
source file consists of the following 3 parts:
1. An OPTIONAL package statement
2. ZERO or more import statements
3. ZERO or more class definitions
Therefore, there's nothing stopping you from compiling
successfully an empty source file (of course, it's absolutely
useless -- but that's another matter entirely).
Hope that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic