I went back to my home directory and typed the following :
It compiled correctly. My question is that A.java has a reference to class B. The compiler searches the classpath for any packages that contains class B. pkg being the only package in the classpath, it doesn't find the B class file there. It should have been the end of story and the code shouldn't have compiled. But what happens is that the B.java file is also compiled. Why ?
The classpath is the current directory. The only sub-directory that has java files or class files is pkg
Abimaran Kugathasan wrote:Oh, your problem is, why does the compiler compile class B even though you haven't explicitly compiled the Class B before Class A? Correct?
Right. And how does it find out which file to compile in order to generate class B ? It must be going through this entire process when it encounters a reference to class B in A.java and doesn't find B.class in the classpath. In fact, why doesn't it just stop here when it can't find class B in the classpath, like it usually does and throw a compile-time error? Why compile B.java ?
Campbell Ritchie wrote:Obviously what confused me about the initial / is that I usually use *nix where an initial / will take you to the root of the drive. The syntax of the / must be different here.
I am really sorry !! I forgot to include ~ before /pkg to denote my current directory which is the home directory. But I made the typing error on this forum only. On the terminal I typed without the / which is equivalent to ~/ in my case. I totally missed it. Sorry again. I am using Linux too.
Shanky Sohar wrote:
Daud Miyan wrote:pkg being the only package in the classpath, it doesn't find the B class file there.
did you see both the classes are in same package,why not you try to shift the class B to different package say pkg2 and then see the result.
There is no class A or class B at the time I am compiling A.java. On compilation, both are appropriately generated in the package pkg. My question is about the mechanism of generation of these class files considering the fact that class B is not present at the time of compiling class A. By what mechanism does javac cause B.java to be implicitly compiled on compiling A.java ?
Daud Miyan wrote: . . . I am really sorry !! I forgot to include ~ before /pkg . . .
Use copy-and-paste; that will obviate such confusion.
We have other hints about copying text: start from this FAQ.