• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question for Mr. Michael Ernest

 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael:
If in a folder, there are only two files: A.java and B.java.
A.java content:
public class A { B b = new B() ; }
B.java content:
public class B { A a = new A() ; }
How can I compile them? Why?
Thanks in advance!
Don
 
morph wang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac A.java B.java
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, just "javac A.java" will compile both. I was wondering why?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Don -
The compiler itself behaves as a java application, which means it has its own sourcepath and classpath properties. When a class reference comes up in compiling a piece of code, javac first attempts to locate its .class reference.
Failing that, it will next try to locate a .java file that contains it, if it's in the sourcepath. By default, the sourcepath is the current directory, so the compiler finds B.java and tries to compile it, so that it can finish compiling A.java.
This reduces the work needed to automate compilation, as in writing a makefile. Using sourcepath, the compiler would also recompile B.java if it appeared to have been modified since the last time it was used.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
Don
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic