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

A Beginner Question?

 
Owee Nicolas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is an empty file a valid source file?
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I deleted the answer I posted until you can confirm that this is not a homework problem.
[ June 16, 2002: Message edited by: John Dale ]
 
Owee Nicolas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
This is not a homework problem. I saw this question in Chapter 2: Language Fundamentals of Mughal's book. I posted it here because there are no Java experts here in our office to answer my question. Although I think the answer is NO. I mean I tried compiling an empty file and it compiled ok but when I ran it, I got a NoClassDefFoundError which only means that this isn't a valid source file. Am I correct?
[ June 17, 2002: Message edited by: owee nicolas ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless I'm not thinking clearly...
I tried compiling an empty file and it compiled ok
That means that it is a valid source file - the source file compiled ok.
but when I ran it, I got a NoClassDefFoundError
That means that it isn't a very useful class file - there is no actual class definition and so it serves no practical use.
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The empty file seems to be a valid source file according to the according to the Java Language Specification (2nd Edition)section 3.5, Input Elements and Tokans, and section 7.3, Compilation Units.
However, when you ask "java" to run a class B, you are asking it to run a class, not a file. It's convention is that, if the class is not alreay loaded, it looks for the class in a file called B.class (among other places). But that class could be defined in a source file by any name, for example, in A.java. I think it is a requirement of some compilers (such as the JDK's javac), that a PUBLIC class must be specified in a .java file that matches the name of the class, but there is no such rule for non-public classes. Hence, it is legal to have a source file A.java that contains non-public class B, and produces a bytecode file called B.class .
 
Owee Nicolas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Got my answer
Thanks for all the response!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic