This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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 ]
Joined: Jun 16, 2002
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 ]
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.
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 .